Class: Events::Subscribers::ActionCableBridge

Inherits:
Object
  • Object
show all
Includes:
Events::Subscriber, Singleton
Defined in:
lib/events/subscribers/action_cable_bridge.rb

Overview

Forwards EventBus events to Action Cable, bridging internal pub/sub to external WebSocket clients. Each event is broadcast to the session-specific stream (e.g. “session_42”), matching the stream name used by SessionChannel.

Only events with a valid session_id are broadcast — events without one have no destination channel and are silently skipped.

Examples:

Events::Bus.subscribe(Events::Subscribers::ActionCableBridge.instance)
# Now all events with session_id flow to "session_<id>" streams

Instance Method Summary collapse

Instance Method Details

#emit(event) ⇒ Object

Receives a Rails.event notification hash and broadcasts the payload to the session’s Action Cable stream.

Parameters:

  • event (Hash)

    with :payload containing event data including :session_id



24
25
26
27
28
29
30
31
32
# File 'lib/events/subscribers/action_cable_bridge.rb', line 24

def emit(event)
  payload = event[:payload]
  return unless payload.is_a?(Hash)

  session_id = payload[:session_id]
  return if session_id.nil?

  ActionCable.server.broadcast("session_#{session_id}", payload)
end