Class: PusherFake::Channel::Presence

Inherits:
Private show all
Defined in:
lib/pusher-fake/channel/presence.rb

Overview

A presence channel.

Constant Summary

Constants inherited from Public

PusherFake::Channel::Public::CACHE_CHANNEL_PREFIX

Instance Attribute Summary collapse

Attributes inherited from Public

#connections, #name

Instance Method Summary collapse

Methods inherited from Private

#add, #authentication_for, #authorized?

Methods inherited from Public

#add, #emit, #includes?, #trigger

Constructor Details

#initialize(name) ⇒ Presence

Create a new PusherFake::Channel::Presence object.

Parameters:

  • name (String)

    The channel name.



13
14
15
16
17
# File 'lib/pusher-fake/channel/presence.rb', line 13

def initialize(name)
  super

  @members = {}
end

Instance Attribute Details

#membersHash (readonly)

Returns Channel members hash.

Returns:

  • (Hash)

    Channel members hash.



8
9
10
# File 'lib/pusher-fake/channel/presence.rb', line 8

def members
  @members
end

Instance Method Details

#remove(connection) ⇒ Object

Remove the connection from the channel and notify the channel.

Also trigger the member_removed webhook.

Parameters:

  • connection (Connection)

    The connection to remove.



24
25
26
27
28
29
30
31
32
33
# File 'lib/pusher-fake/channel/presence.rb', line 24

def remove(connection)
  super

  return unless members.key?(connection)

  trigger("member_removed",
          channel: name, user_id: members[connection][:user_id])

  emit("pusher_internal:member_removed", members.delete(connection))
end

#subscription_dataHash

Return a hash containing presence information for the channel.

Returns:

  • (Hash)

    Hash containing presence information.



38
39
40
41
42
# File 'lib/pusher-fake/channel/presence.rb', line 38

def subscription_data
  hash = members.to_h { |_, member| [member[:user_id], member[:user_info]] }

  { presence: { hash: hash, count: members.size } }
end