class PusherFake::Channel::Public
A public channel.
Attributes
@return [Array] Connections in this channel.
@return [String] The channel name.
Public Class Methods
Create a new {Public} object.
@param [String] name The channel name.
# File lib/pusher-fake/channel/public.rb, line 16 def initialize(name) @name = name @connections = [] end
Public Instance Methods
Add the connection to the channel.
@param [Connection] connection The connection to add. @param [Hash] options The options for the channel.
# File lib/pusher-fake/channel/public.rb, line 25 def add(connection, options = {}) subscription_succeeded(connection, options) end
Emit an event to the channel.
@param [String] event The event name. @param [Hash] data The event data.
# File lib/pusher-fake/channel/public.rb, line 33 def emit(event, data, options = {}) connections.each do |connection| unless connection.id == options[:socket_id] connection.emit(event, data, name) end end end
Determine if the connection
is in the channel.
@param [Connection] connection The connection. @return [Boolean] If the connection is in the channel or not.
# File lib/pusher-fake/channel/public.rb, line 45 def includes?(connection) connections.index(connection) end
Remove the connection
from the channel.
If it is the last connection, trigger the channel_vacated webhook.
@param [Connection] connection The connection to remove.
# File lib/pusher-fake/channel/public.rb, line 54 def remove(connection) connections.delete(connection) trigger("channel_vacated", channel: name) if connections.empty? end
Return subscription data for the channel.
@abstract @return [Hash] Subscription data for the channel.
# File lib/pusher-fake/channel/public.rb, line 64 def subscription_data {} end
# File lib/pusher-fake/channel/public.rb, line 68 def trigger(name, data = {}) PusherFake::Webhook.trigger(name, data) end
Private Instance Methods
Notify the connection
of the successful subscription and add the connection to the channel.
If it is the first connection, trigger the channel_occupied webhook.
@param [Connection] connection Connection
a subscription succeeded for. @param [Hash] options The options for the channel.
# File lib/pusher-fake/channel/public.rb, line 81 def subscription_succeeded(connection, _options = {}) connection.emit("pusher_internal:subscription_succeeded", subscription_data, name) connections.push(connection) trigger("channel_occupied", channel: name) if connections.length == 1 end