Class: Rubycent::Client
- Inherits:
-
Object
- Object
- Rubycent::Client
- Defined in:
- lib/rubycent/client.rb
Overview
Rubycent::Client
Main object that handles configuration and requests to centrifugo API
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#host ⇒ Object
Returns the value of attribute host.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#port ⇒ Object
Returns the value of attribute port.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#broadcast(channels, data) ⇒ Hash
Publish data into multiple channels (Similar to ‘#publish` but allows to send the same data into many channels).
-
#channels ⇒ Hash
Get list of active(with one or more subscribers) channels.
-
#disconnect(user_id) ⇒ Hash
Disconnect user by it’s ID.
-
#history(channel) ⇒ Hash
Get channel history information (list of last messages published into channel).
-
#info ⇒ Hash
Get information about running Centrifugo nodes.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#issue_channel_token(client, channel, expiration = nil, info = {}, algorithm = 'HS256') ⇒ String
Generate JWT for private channels.
-
#issue_user_token(user_id, expiration = nil, info = {}, algorithm = 'HS256') ⇒ String
Generate connection JWT for the given user.
-
#presence(channel) ⇒ Hash
Get channel presence information (all clients currently subscribed on this channel).
-
#presence_stats(channel) ⇒ Hash
Get short channel presence information.
-
#publish(channel, data) ⇒ Hash
Publish data into channel.
-
#unsubscribe(channel, user_id) ⇒ Hash
Unsubscribe user from channel.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubycent/client.rb', line 60 def initialize( = {}) = DEFAULT_OPTIONS.merge() @scheme, @host, @port, @secret, @api_key = .values_at( :scheme, :host, :port, :secret, :api_key ) @timeout = 5 @open_timeout = 5 end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
21 22 23 |
# File 'lib/rubycent/client.rb', line 21 def api_key @api_key end |
#host ⇒ Object
Returns the value of attribute host.
21 22 23 |
# File 'lib/rubycent/client.rb', line 21 def host @host end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
23 24 25 |
# File 'lib/rubycent/client.rb', line 23 def open_timeout @open_timeout end |
#port ⇒ Object
Returns the value of attribute port.
21 22 23 |
# File 'lib/rubycent/client.rb', line 21 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
21 22 23 |
# File 'lib/rubycent/client.rb', line 21 def scheme @scheme end |
#secret ⇒ Object
Returns the value of attribute secret.
21 22 23 |
# File 'lib/rubycent/client.rb', line 21 def secret @secret end |
#timeout ⇒ Object
Returns the value of attribute timeout.
23 24 25 |
# File 'lib/rubycent/client.rb', line 23 def timeout @timeout end |
Instance Method Details
#broadcast(channels, data) ⇒ Hash
Publish data into multiple channels
(Similar to `#publish` but allows to send the same data into many channels)
107 108 109 |
# File 'lib/rubycent/client.rb', line 107 def broadcast(channels, data) construct_query.execute('broadcast', channels: channels, data: data) end |
#channels ⇒ Hash
Get list of active(with one or more subscribers) channels.
259 260 261 |
# File 'lib/rubycent/client.rb', line 259 def channels construct_query.execute('channels', {}) end |
#disconnect(user_id) ⇒ Hash
Disconnect user by it’s ID
146 147 148 |
# File 'lib/rubycent/client.rb', line 146 def disconnect(user_id) construct_query.execute('disconnect', user: user_id) end |
#history(channel) ⇒ Hash
Get channel history information
(list of last published into channel)
237 238 239 |
# File 'lib/rubycent/client.rb', line 237 def history(channel) construct_query.execute('history', channel: channel) end |
#info ⇒ Hash
Get information about running Centrifugo nodes
289 290 291 |
# File 'lib/rubycent/client.rb', line 289 def info construct_query.execute('info', {}) end |
#issue_channel_token(client, channel, expiration = nil, info = {}, algorithm = 'HS256') ⇒ String
At moment the only supported JWT algorithm is HS256 - i.e. HMAC SHA-256. This can be extended later.
Generate JWT for private channels
355 356 357 |
# File 'lib/rubycent/client.rb', line 355 def issue_channel_token(client, channel, expiration = nil, info = {}, algorithm = 'HS256') issue_token({ 'client' => client, 'channel' => channel }, expiration, info, algorithm) end |
#issue_user_token(user_id, expiration = nil, info = {}, algorithm = 'HS256') ⇒ String
At moment the only supported JWT algorithm is HS256 - i.e. HMAC SHA-256. This can be extended later.
Generate connection JWT for the given user
322 323 324 |
# File 'lib/rubycent/client.rb', line 322 def issue_user_token(user_id, expiration = nil, info = {}, algorithm = 'HS256') issue_token({ 'sub' => user_id }, expiration, info, algorithm) end |
#presence(channel) ⇒ Hash
Get channel presence information
(all clients currently subscribed on this channel)
178 179 180 |
# File 'lib/rubycent/client.rb', line 178 def presence(channel) construct_query.execute('presence', channel: channel) end |
#presence_stats(channel) ⇒ Hash
Get short channel presence information
201 202 203 |
# File 'lib/rubycent/client.rb', line 201 def presence_stats(channel) construct_query.execute('presence_stats', channel: channel) end |
#publish(channel, data) ⇒ Hash
Publish data into channel
88 89 90 |
# File 'lib/rubycent/client.rb', line 88 def publish(channel, data) construct_query.execute('publish', channel: channel, data: data) end |
#unsubscribe(channel, user_id) ⇒ Hash
Unsubscribe user from channel
128 129 130 |
# File 'lib/rubycent/client.rb', line 128 def unsubscribe(channel, user_id) construct_query.execute('unsubscribe', channel: channel, user: user_id) end |