Class: Faye::Redis::SubscriptionManager
- Inherits:
-
Object
- Object
- Faye::Redis::SubscriptionManager
- Defined in:
- lib/faye/redis/subscription_manager.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#channel_matches_pattern?(channel, pattern) ⇒ Boolean
Check if a channel matches a pattern.
-
#cleanup_client_subscriptions(client_id) ⇒ Object
Clean up subscriptions for a client.
-
#get_client_subscriptions(client_id, &callback) ⇒ Object
Get all channels a client is subscribed to.
-
#get_pattern_subscribers(channel) ⇒ Object
Get subscribers matching wildcard patterns.
-
#get_subscribers(channel, &callback) ⇒ Object
Get all clients subscribed to a channel.
-
#initialize(connection, options = {}) ⇒ SubscriptionManager
constructor
A new instance of SubscriptionManager.
-
#subscribe(client_id, channel, &callback) ⇒ Object
Subscribe a client to a channel.
-
#unsubscribe(client_id, channel, &callback) ⇒ Object
Unsubscribe a client from a channel.
-
#unsubscribe_all(client_id, &callback) ⇒ Object
Unsubscribe a client from all channels.
Constructor Details
#initialize(connection, options = {}) ⇒ SubscriptionManager
Returns a new instance of SubscriptionManager.
6 7 8 9 |
# File 'lib/faye/redis/subscription_manager.rb', line 6 def initialize(connection, = {}) @connection = connection @options = end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
4 5 6 |
# File 'lib/faye/redis/subscription_manager.rb', line 4 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/faye/redis/subscription_manager.rb', line 4 def @options end |
Instance Method Details
#channel_matches_pattern?(channel, pattern) ⇒ Boolean
Check if a channel matches a pattern
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/faye/redis/subscription_manager.rb', line 149 def channel_matches_pattern?(channel, pattern) # Convert Faye wildcard pattern to regex # * matches one segment, ** matches multiple segments regex_pattern = pattern .gsub('**', '__DOUBLE_STAR__') .gsub('*', '[^/]+') .gsub('__DOUBLE_STAR__', '.*') regex = Regexp.new("^#{regex_pattern}$") !!(channel =~ regex) end |
#cleanup_client_subscriptions(client_id) ⇒ Object
Clean up subscriptions for a client
162 163 164 |
# File 'lib/faye/redis/subscription_manager.rb', line 162 def cleanup_client_subscriptions(client_id) unsubscribe_all(client_id) end |
#get_client_subscriptions(client_id, &callback) ⇒ Object
Get all channels a client is subscribed to
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/faye/redis/subscription_manager.rb', line 93 def get_client_subscriptions(client_id, &callback) channels = @connection.with_redis do |redis| redis.smembers(client_subscriptions_key(client_id)) end EventMachine.next_tick { callback.call(channels) } if callback channels rescue => e log_error("Failed to get subscriptions for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call([]) } if callback [] end |
#get_pattern_subscribers(channel) ⇒ Object
Get subscribers matching wildcard patterns
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/faye/redis/subscription_manager.rb', line 127 def get_pattern_subscribers(channel) patterns = @connection.with_redis do |redis| redis.smembers(patterns_key) end matching_clients = [] patterns.each do |pattern| if channel_matches_pattern?(channel, pattern) clients = @connection.with_redis do |redis| redis.smembers(channel_subscribers_key(pattern)) end matching_clients.concat(clients) end end matching_clients.uniq rescue => e log_error("Failed to get pattern subscribers for channel #{channel}: #{e.}") [] end |
#get_subscribers(channel, &callback) ⇒ Object
Get all clients subscribed to a channel
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/faye/redis/subscription_manager.rb', line 107 def get_subscribers(channel, &callback) # Get direct subscribers direct_subscribers = @connection.with_redis do |redis| redis.smembers(channel_subscribers_key(channel)) end # Get pattern subscribers pattern_subscribers = get_pattern_subscribers(channel) all_subscribers = (direct_subscribers + pattern_subscribers).uniq EventMachine.next_tick { callback.call(all_subscribers) } if callback all_subscribers rescue => e log_error("Failed to get subscribers for channel #{channel}: #{e.}") EventMachine.next_tick { callback.call([]) } if callback [] end |
#subscribe(client_id, channel, &callback) ⇒ Object
Subscribe a client to a channel
12 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 42 |
# File 'lib/faye/redis/subscription_manager.rb', line 12 def subscribe(client_id, channel, &callback) = Time.now.to_i @connection.with_redis do |redis| redis.multi do |multi| # Add channel to client's subscriptions multi.sadd?(client_subscriptions_key(client_id), channel) # Add client to channel's subscribers multi.sadd?(channel_subscribers_key(channel), client_id) # Store subscription metadata multi.hset( subscription_key(client_id, channel), 'subscribed_at', , 'channel', channel, 'client_id', client_id ) # Handle wildcard patterns if channel.include?('*') multi.sadd?(patterns_key, channel) end end end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to subscribe client #{client_id} to #{channel}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#unsubscribe(client_id, channel, &callback) ⇒ Object
Unsubscribe a client from a channel
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/faye/redis/subscription_manager.rb', line 45 def unsubscribe(client_id, channel, &callback) @connection.with_redis do |redis| redis.multi do |multi| # Remove channel from client's subscriptions multi.srem?(client_subscriptions_key(client_id), channel) # Remove client from channel's subscribers multi.srem?(channel_subscribers_key(channel), client_id) # Delete subscription metadata multi.del(subscription_key(client_id, channel)) end end # Clean up wildcard pattern if no more subscribers if channel.include?('*') cleanup_pattern_if_unused(channel) end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to unsubscribe client #{client_id} from #{channel}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#unsubscribe_all(client_id, &callback) ⇒ Object
Unsubscribe a client from all channels
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/faye/redis/subscription_manager.rb', line 71 def unsubscribe_all(client_id, &callback) # Get all channels the client is subscribed to get_client_subscriptions(client_id) do |channels| if channels.empty? callback.call(true) if callback else # Unsubscribe from each channel remaining = channels.size channels.each do |channel| unsubscribe(client_id, channel) do remaining -= 1 callback.call(true) if callback && remaining == 0 end end end end rescue => e log_error("Failed to unsubscribe client #{client_id} from all channels: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |