Module: LiteCable::Channel::Streams

Included in:
Base
Defined in:
lib/lite_cable/channel/streams.rb

Overview

Most commonly, the streamed broadcast is sent straight to the subscriber on the client-side. The channel just acts as a connector between the two parties (the broadcaster and the channel subscriber). Here’s an example of a channel that allows subscribers to get all new comments on a given page:

class CommentsChannel < ApplicationCable::Channel
  def follow(data)
    stream_from "comments_for_#{data['recording_id']}"
  end

  def unfollow
    stop_all_streams
  end
end

Based on the above example, the subscribers of this channel will get whatever data is put into the, let’s say, ‘comments_for_45` broadcasting as soon as it’s put there.

An example broadcasting for this channel looks like so:

LiteCable.server.broadcast "comments_for_45", author: 'Donald Duck', content: 'Quack-quack-quack'

You can stop streaming from all broadcasts by calling #stop_all_streams or use #stop_from to stop streaming broadcasts from the specified stream.

Instance Method Summary collapse

Instance Method Details

#handle_unsubscribeObject

rubocop:enable Metrics/LineLength



33
34
35
36
# File 'lib/lite_cable/channel/streams.rb', line 33

def handle_unsubscribe
  stop_all_streams
  super
end

#stop_all_streamsObject

Unsubscribes all streams associated with this channel from the pubsub queue.



51
52
53
54
# File 'lib/lite_cable/channel/streams.rb', line 51

def stop_all_streams
  log(:debug) { log_fmt("Stop all streams") }
  connection.streams.remove_all(identifier)
end

#stop_stream(broadcasting) ⇒ Object

Stop streaming from the named broadcasting pubsub queue.



45
46
47
48
# File 'lib/lite_cable/channel/streams.rb', line 45

def stop_stream(broadcasting)
  log(:debug) { log_fmt("Stop stream from #{broadcasting}") }
  connection.streams.remove(identifier, broadcasting)
end

#stream_from(broadcasting) ⇒ Object

Start streaming from the named broadcasting pubsub queue.



39
40
41
42
# File 'lib/lite_cable/channel/streams.rb', line 39

def stream_from(broadcasting)
  log(:debug) { log_fmt("Stream from #{broadcasting}") }
  connection.streams.add(identifier, broadcasting)
end