Class: MagicBus::Subscriber
- Inherits:
-
Object
- Object
- MagicBus::Subscriber
- Defined in:
- lib/magicbus/subscriber.rb
Class Attribute Summary collapse
-
.channels ⇒ Object
Returns the value of attribute channels.
-
.clients ⇒ Object
Returns the value of attribute clients.
-
.connection ⇒ Object
Returns the value of attribute connection.
Class Method Summary collapse
Class Attribute Details
.channels ⇒ Object
Returns the value of attribute channels.
5 6 7 |
# File 'lib/magicbus/subscriber.rb', line 5 def channels @channels end |
.clients ⇒ Object
Returns the value of attribute clients.
6 7 8 |
# File 'lib/magicbus/subscriber.rb', line 6 def clients @clients end |
.connection ⇒ Object
Returns the value of attribute connection.
4 5 6 |
# File 'lib/magicbus/subscriber.rb', line 4 def connection @connection end |
Class Method Details
.subscribe(user_id, out) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/magicbus/subscriber.rb', line 13 def self.subscribe(user_id, out) user_id = user_id.to_s if channels[user_id].nil? || channels[user_id].status == :closed channels[user_id] = AMQP::Channel.new(MagicBus.connection) end channel = channels[user_id] exchange = channel.fanout(user_id) client_id = SecureRandom.uuid queue = channel.queue(client_id) .bind(exchange).subscribe do |payload| out << "data: #{payload}\n\n" end self.clients[user_id] ||= {} self.clients[user_id][client_id] = queue client_id end |
.unsubscribe(user_id, client_id) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/magicbus/subscriber.rb', line 43 def self.unsubscribe(user_id, client_id) user_id = user_id.to_s if self.clients[user_id][client_id] self.clients[user_id][client_id] = nil end end |