Module: WhereIsWaldo::Broadcastable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/where_is_waldo/broadcastable.rb

Overview

Opt a model into real-time broadcasting. Include the concern and declare which lifecycle events should push over the cable:

class Person < ApplicationRecord
include WhereIsWaldo::Broadcastable
broadcasts_realtime                                   # create + update + destroy
end

broadcasts_realtime on: %i[create update]               # subset
broadcasts_realtime on: :update, if: -> { saved_change_to_status? }
broadcasts_realtime scope: ->(rec) { rec.organization.users }

Event names follow "_" — created / update / destroyed — e.g. person_created, person_update, person_destroyed (matching the convention issuesbyscott already uses). The client subscribes with useWaldoEvent.

Audience defaults to WhereIsWaldo.configuration.broadcast_audience (set once per app, e.g. ->(rec) { rec.account.users }); override per model with scope:. Payload defaults to a minimal { id: } (so clients refetch); override realtime_payload on the model to push a richer shape for patch-in-place rendering.

Constant Summary collapse

HOOKS =
{
  create: %i[after_create_commit created],
  update: %i[after_update_commit update],
  destroy: %i[after_destroy_commit destroyed]
}.freeze

Instance Method Summary collapse

Instance Method Details

#realtime_payloadObject

Override on the model to shape the payload (default: minimal { id: }).



48
49
50
# File 'app/models/concerns/where_is_waldo/broadcastable.rb', line 48

def realtime_payload
  { id: id }
end