Class: Hippo::API::PubSub

Inherits:
ActionCable::Channel::Base
  • Object
show all
Defined in:
lib/hippo/api/pub_sub.rb

Constant Summary collapse

PREFIX =
'ps:'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.channel_prefixObject


28
29
30
# File 'lib/hippo/api/pub_sub.rb', line 28

def self.channel_prefix
    PREFIX
end

.publish(channel, data) ⇒ Object


23
24
25
26
# File 'lib/hippo/api/pub_sub.rb', line 23

def self.publish(channel, data)
    channel = channel_prefix + channel
    ActionCable.server.broadcast(channel, data.merge(channel: channel))
end

Instance Method Details

#channel_prefixObject


32
33
34
# File 'lib/hippo/api/pub_sub.rb', line 32

def channel_prefix
    PREFIX
end

#off(data) ⇒ Object


15
16
17
18
19
20
21
# File 'lib/hippo/api/pub_sub.rb', line 15

def off(data)
    channel = channel_prefix + data['channel']
    subscribers = pubsub
                      .instance_variable_get('@listener')
                      .instance_variable_get('@subscribers')[channel]
    pubsub.unsubscribe(channel, subscribers.first) if subscribers.any?
end

#on(data) ⇒ Object


10
11
12
13
# File 'lib/hippo/api/pub_sub.rb', line 10

def on(data)
    Hippo.logger.info "pubsub  on: #{data['channel']}"
    stream_from channel_prefix + data['channel']
end