Class: Ki::ChannelManager
Class Method Summary collapse
- .cleanup ⇒ Object
- .connect ⇒ Object
- .db ⇒ Object
- .disconnect(socket) ⇒ Object
- .publish(json) ⇒ Object
- .sockets ⇒ Object
- .subscribe(json) ⇒ Object
- .tick(json) ⇒ Object
- .unsubscribe(json) ⇒ Object
Class Method Details
.cleanup ⇒ Object
50 51 52 53 54 |
# File 'lib/ki/channel_manager.rb', line 50 def self.cleanup db.delete 'realtime_channel_sockets', {} db.delete 'realtime_channel_subscriptions', {} db.delete 'realtime_channel_messages', {} end |
.connect ⇒ Object
5 6 7 |
# File 'lib/ki/channel_manager.rb', line 5 def self.connect db.insert 'realtime_channel_sockets', {} end |
.disconnect(socket) ⇒ Object
9 10 11 12 |
# File 'lib/ki/channel_manager.rb', line 9 def self.disconnect(socket) db.delete 'realtime_channel_sockets', { 'id' => socket['id'] } db.delete 'realtime_channel_subscriptions', { 'socket_id' => socket['id'] } end |
.publish(json) ⇒ Object
32 33 34 35 |
# File 'lib/ki/channel_manager.rb', line 32 def self.publish(json) json['created_at'] = Time.now.to_i db.insert 'realtime_channel_messages', json end |
.sockets ⇒ Object
14 15 16 |
# File 'lib/ki/channel_manager.rb', line 14 def self.sockets db.find 'realtime_channel_sockets' end |
.subscribe(json) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/ki/channel_manager.rb', line 18 def self.subscribe(json) channels = db.find 'realtime_channel_subscriptions', json if channels.empty? item = db.insert 'realtime_channel_subscriptions', json channels.push item end channels end |
.tick(json) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ki/channel_manager.rb', line 37 def self.tick(json) subscribed_channels = db.find 'realtime_channel_subscriptions', json channel_names = subscribed_channels.map { |sc| sc['channel_name'] } t = Time.now.to_i = db.find 'realtime_channel_messages', { 'channel_name' => { '$in' => channel_names }, 'created_at' => { '$gt' => t - 5 } } end |
.unsubscribe(json) ⇒ Object
27 28 29 30 |
# File 'lib/ki/channel_manager.rb', line 27 def self.unsubscribe(json) json.delete('type') db.delete 'realtime_channel_subscriptions', json end |