Class: WhereIsWaldo::Broadcaster

Inherits:
Object
  • Object
show all
Defined in:
app/services/where_is_waldo/broadcaster.rb

Class Method Summary collapse

Class Method Details

.broadcast_to(scope, message_type, data = {}) ⇒ Object

Broadcast a message to subjects in an AR scope

Parameters:

  • scope (ActiveRecord::Relation, ActiveRecord::Base)

    Subjects to broadcast to

  • message_type (String, Symbol)

    Message type for client handler routing

  • data (Hash) (defaults to: {})

    Message payload



10
11
12
13
14
15
16
17
18
19
# File 'app/services/where_is_waldo/broadcaster.rb', line 10

def broadcast_to(scope, message_type, data = {})
  subject_ids = extract_subject_ids(scope)
  return if subject_ids.empty?

  message = build_message(message_type, data)

  subject_ids.each do |subject_id|
    ActionCable.server.broadcast(subject_stream(subject_id), message)
  end
end

.broadcast_to_online(scope, message_type, data = {}) ⇒ Object

Broadcast to online subjects only (from an AR scope)

Parameters:

  • scope (ActiveRecord::Relation)

    Scope to filter

  • message_type (String, Symbol)

    Message type

  • data (Hash) (defaults to: {})

    Message payload



25
26
27
28
29
30
31
32
33
34
# File 'app/services/where_is_waldo/broadcaster.rb', line 25

def broadcast_to_online(scope, message_type, data = {})
  online_ids = PresenceService.online_ids(scope)
  return if online_ids.empty?

  message = build_message(message_type, data)

  online_ids.each do |subject_id|
    ActionCable.server.broadcast(subject_stream(subject_id), message)
  end
end

.broadcast_to_session(session_id, message_type, data = {}) ⇒ Object

Broadcast to a specific session

Parameters:

  • session_id (String)

    Session identifier

  • message_type (String, Symbol)

    Message type

  • data (Hash) (defaults to: {})

    Message payload



40
41
42
43
44
45
46
47
# File 'app/services/where_is_waldo/broadcaster.rb', line 40

def broadcast_to_session(session_id, message_type, data = {})
  status = PresenceService.session_status(session_id)
  return false unless status

  message = build_message(message_type, data, target_session: session_id)
  ActionCable.server.broadcast(subject_stream(status[:subject_id]), message)
  true
end