instance method subscribe

Ruby on Rails 8.1.2

Since v8.1.2

Signature

subscribe(subscriber, &filter)

Registers a new event subscriber. The subscriber must respond to

emit(event: Hash)

The event hash will have the following keys:

name: String (The name of the event)
payload: Hash, Object (The payload of the event, or the event object itself)
tags: Hash (The tags of the event)
context: Hash (The context of the event)
timestamp: Float (The timestamp of the event, in nanoseconds)
source_location: Hash (The source location of the event, containing the filepath, lineno, and label)

An optional filter proc can be provided to only receive a subset of events:

Rails.event.subscribe(subscriber) { |event| event[:name].start_with?("user.") }
Rails.event.subscribe(subscriber) { |event| event[:payload].is_a?(UserEvent) }

Parameters

subscriber req
filter block
Source
# File activesupport/lib/active_support/event_reporter.rb, line 311
    def subscribe(subscriber, &filter)
      unless subscriber.respond_to?(:emit)
        raise ArgumentError, "Event subscriber #{subscriber.class.name} must respond to #emit"
      end
      @subscribers << { subscriber: subscriber, filter: filter }
    end

Defined in activesupport/lib/active_support/event_reporter.rb line 311 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::EventReporter

Type at least 2 characters to search.

↑↓ navigate · open · esc close