Class: Cent::Client
- Inherits:
-
Object
- Object
- Cent::Client
- Defined in:
- lib/cent/client.rb
Overview
Cent::Client
Main object that handles configuration and requests to centrifugo API
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:) ⇒ 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(api_key:, endpoint: 'http://localhost:8000/api') {|Faraday::Connection| ... } ⇒ Client
constructor
A new instance of Client.
-
#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:) ⇒ Hash
Unsubscribe user from channel.
Constructor Details
#initialize(api_key:, endpoint: 'http://localhost:8000/api') {|Faraday::Connection| ... } ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cent/client.rb', line 27 def initialize(api_key:, endpoint: 'http://localhost:8000/api') headers = { 'Content-Type' => 'application/json', 'Authorization' => "apikey #{api_key}" } @connection = Faraday.new(endpoint, headers: headers) do |conn| conn.request :json # encode req bodies as JSON conn.response :json # decode response bodies as JSON conn.response :raise_error yield conn if block_given? end 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)
79 80 81 |
# File 'lib/cent/client.rb', line 79 def broadcast(channels:, data:) execute('broadcast', channels: channels, data: data) end |
#channels ⇒ Hash
Get list of active(with one or more subscribers) channels.
231 232 233 |
# File 'lib/cent/client.rb', line 231 def channels execute('channels', {}) end |
#disconnect(user:) ⇒ Hash
Disconnect user by it’s ID
118 119 120 |
# File 'lib/cent/client.rb', line 118 def disconnect(user:) execute('disconnect', user: user) end |
#history(channel:) ⇒ Hash
Get channel history information
(list of last published into channel)
209 210 211 |
# File 'lib/cent/client.rb', line 209 def history(channel:) execute('history', channel: channel) end |
#info ⇒ Hash
Get information about running Centrifugo nodes
261 262 263 |
# File 'lib/cent/client.rb', line 261 def info execute('info', {}) end |
#presence(channel:) ⇒ Hash
Get channel presence information
(all clients currently subscribed on this channel)
150 151 152 |
# File 'lib/cent/client.rb', line 150 def presence(channel:) execute('presence', channel: channel) end |
#presence_stats(channel:) ⇒ Hash
Get short channel presence information
173 174 175 |
# File 'lib/cent/client.rb', line 173 def presence_stats(channel:) execute('presence_stats', channel: channel) end |
#publish(channel:, data:) ⇒ Hash
Publish data into channel
60 61 62 |
# File 'lib/cent/client.rb', line 60 def publish(channel:, data:) execute('publish', channel: channel, data: data) end |
#unsubscribe(channel:, user:) ⇒ Hash
Unsubscribe user from channel
100 101 102 |
# File 'lib/cent/client.rb', line 100 def unsubscribe(channel:, user:) execute('unsubscribe', channel: channel, user: user) end |