Class: Faye::Redis
- Inherits:
-
Object
- Object
- Faye::Redis
- Defined in:
- lib/faye/redis.rb
Constant Summary collapse
- DEFAULT_HOST =
'localhost'
- DEFAULT_PORT =
6379
- DEFAULT_DATABASE =
0
- DEFAULT_GC =
60
- LOCK_TIMEOUT =
120
Class Method Summary collapse
Instance Method Summary collapse
- #after_destroy(client_id, &callback) ⇒ Object
- #client_exists(client_id, &callback) ⇒ Object
- #create_client(&callback) ⇒ Object
- #destroy_client(client_id, &callback) ⇒ Object
- #disconnect ⇒ Object
- #empty_queue(client_id) ⇒ Object
- #init ⇒ Object
-
#initialize(server, options) ⇒ Redis
constructor
A new instance of Redis.
- #ping(client_id) ⇒ Object
- #publish(message, channels) ⇒ Object
- #subscribe(client_id, channel, &callback) ⇒ Object
- #unsubscribe(client_id, channel, &callback) ⇒ Object
Constructor Details
#initialize(server, options) ⇒ Redis
Returns a new instance of Redis.
17 18 19 20 |
# File 'lib/faye/redis.rb', line 17 def initialize(server, ) @server = server @options = end |
Class Method Details
.create(server, options) ⇒ Object
13 14 15 |
# File 'lib/faye/redis.rb', line 13 def self.create(server, ) new(server, ) end |
Instance Method Details
#after_destroy(client_id, &callback) ⇒ Object
90 91 92 93 94 |
# File 'lib/faye/redis.rb', line 90 def after_destroy(client_id, &callback) @server.debug 'Destroyed client ?', client_id @server.trigger(:disconnect, client_id) callback.call if callback end |
#client_exists(client_id, &callback) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/faye/redis.rb', line 96 def client_exists(client_id, &callback) init @redis.zscore(@ns + '/clients', client_id) do |score| callback.call(score != nil) end end |
#create_client(&callback) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/faye/redis.rb', line 60 def create_client(&callback) init client_id = @server.generate_id @redis.zadd(@ns + '/clients', 0, client_id) do |added| next create_client(&callback) if added == 0 @server.debug 'Created new client ?', client_id ping(client_id) @server.trigger(:handshake, client_id) callback.call(client_id) end end |
#destroy_client(client_id, &callback) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/faye/redis.rb', line 72 def destroy_client(client_id, &callback) init @redis.zrem(@ns + '/clients', client_id) @redis.del(@ns + "/clients/#{client_id}/messages") @redis.smembers(@ns + "/clients/#{client_id}/channels") do |channels| i, n = 0, channels.size next after_destroy(client_id, &callback) if i == n channels.each do |channel| unsubscribe(client_id, channel) do i += 1 after_destroy(client_id, &callback) if i == n end end end end |
#disconnect ⇒ Object
55 56 57 58 |
# File 'lib/faye/redis.rb', line 55 def disconnect @subscriber.unsubscribe(@ns + '/notifications') EventMachine.cancel_timer(@gc) end |
#empty_queue(client_id) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/faye/redis.rb', line 154 def empty_queue(client_id) return unless @server.has_connection?(client_id) init key = @ns + "/clients/#{client_id}/messages" @redis.multi @redis.lrange(key, 0, -1) @redis.del(key) @redis.exec.callback do |, deleted| = .map { |json| Yajl::Parser.parse(json) } @server.deliver(client_id, ) end end |
#init ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/faye/redis.rb', line 22 def init return if @redis host = @options[:host] || DEFAULT_HOST port = @options[:port] || DEFAULT_PORT db = @options[:database] || DEFAULT_DATABASE auth = @options[:password] gc = @options[:gc] || DEFAULT_GC @ns = @options[:namespace] || '' socket = @options[:socket] if socket @redis = EventMachine::Hiredis::Client.connect(socket, nil) @subscriber = EventMachine::Hiredis::Client.connect(socket, nil) else @redis = EventMachine::Hiredis::Client.connect(host, port) @subscriber = EventMachine::Hiredis::Client.connect(host, port) end if auth @redis.auth(auth) @subscriber.auth(auth) end @redis.select(db) @subscriber.select(db) @subscriber.subscribe(@ns + '/notifications') @subscriber.on(:message) do |topic, | empty_queue() if topic == @ns + '/notifications' end @gc = EventMachine.add_periodic_timer(gc, &method(:gc)) end |
#ping(client_id) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/faye/redis.rb', line 103 def ping(client_id) init timeout = @server.timeout return unless Numeric === timeout time = get_current_time @server.debug 'Ping ?, ?', client_id, time @redis.zadd(@ns + '/clients', time, client_id) end |
#publish(message, channels) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/faye/redis.rb', line 135 def publish(, channels) init @server.debug 'Publishing message ?', = Yajl::Encoder.encode() channels = Channel.(['channel']) keys = channels.map { |c| @ns + "/channels#{c}" } @redis.sunion(*keys) do |clients| clients.each do |client_id| @server.debug 'Queueing for client ?: ?', client_id, @redis.rpush(@ns + "/clients/#{client_id}/messages", ) @redis.publish(@ns + '/notifications', client_id) end end @server.trigger(:publish, ['clientId'], ['channel'], ['data']) end |
#subscribe(client_id, channel, &callback) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/faye/redis.rb', line 113 def subscribe(client_id, channel, &callback) init @redis.sadd(@ns + "/clients/#{client_id}/channels", channel) do |added| @server.trigger(:subscribe, client_id, channel) if added == 1 end @redis.sadd(@ns + "/channels#{channel}", client_id) do @server.debug 'Subscribed client ? to channel ?', client_id, channel callback.call if callback end end |
#unsubscribe(client_id, channel, &callback) ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/faye/redis.rb', line 124 def unsubscribe(client_id, channel, &callback) init @redis.srem(@ns + "/clients/#{client_id}/channels", channel) do |removed| @server.trigger(:unsubscribe, client_id, channel) if removed == 1 end @redis.srem(@ns + "/channels#{channel}", client_id) do @server.debug 'Unsubscribed client ? from channel ?', client_id, channel callback.call if callback end end |