Class: WebsocketMessaging::Notifier
- Inherits:
-
Struct
- Object
- Struct
- WebsocketMessaging::Notifier
- Defined in:
- lib/websocket_messaging/notifier.rb
Instance Attribute Summary collapse
-
#channel_id ⇒ Object
Returns the value of attribute channel_id.
Class Method Summary collapse
Instance Method Summary collapse
- #connect_to_bus ⇒ Object
- #multicast(channel_ids) ⇒ Object
- #notify(message) ⇒ Object
- #subscribe(&block) ⇒ Object
- #unsubscribe ⇒ Object
Instance Attribute Details
#channel_id ⇒ Object
Returns the value of attribute channel_id
7 8 9 |
# File 'lib/websocket_messaging/notifier.rb', line 7 def channel_id @channel_id end |
Class Method Details
.bus_connector ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/websocket_messaging/notifier.rb', line 40 def self.bus_connector unless @bus_connector raise BusConnectorNotDefinedError, %[you have to define a bus connector, i.e. returning a new connection to redis] end @bus_connector end |
.bus_connector=(connector) ⇒ Object
36 37 38 |
# File 'lib/websocket_messaging/notifier.rb', line 36 def self.bus_connector=(connector) @bus_connector = connector end |
Instance Method Details
#connect_to_bus ⇒ Object
32 33 34 |
# File 'lib/websocket_messaging/notifier.rb', line 32 def connect_to_bus self.class.bus_connector.call end |
#multicast(channel_ids) ⇒ Object
19 20 21 |
# File 'lib/websocket_messaging/notifier.rb', line 19 def multicast(channel_ids) MulticastNotifier.new(channel_ids, self.class.public_method(:new)) end |
#notify(message) ⇒ Object
23 24 25 26 |
# File 'lib/websocket_messaging/notifier.rb', line 23 def notify() return unless && .respond_to?(:to_json) connect_to_bus.publish(channel_id, .to_json) end |
#subscribe(&block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/websocket_messaging/notifier.rb', line 8 def subscribe(&block) @subscription = Thread.new do connect_to_bus.subscribe(channel_id) do |bus| bus. do |channel, | data = JSON.parse() block.call(data) end end end end |
#unsubscribe ⇒ Object
28 29 30 |
# File 'lib/websocket_messaging/notifier.rb', line 28 def unsubscribe @subscription.kill end |