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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cent/client.rb', line 26 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)
78 79 80 |
# File 'lib/cent/client.rb', line 78 def broadcast(channels:, data:) execute('broadcast', channels: channels, data: data) end |
#channels ⇒ Hash
Get list of active(with one or more subscribers) channels.
230 231 232 |
# File 'lib/cent/client.rb', line 230 def channels execute('channels', {}) end |
#disconnect(user:) ⇒ Hash
Disconnect user by it’s ID
117 118 119 |
# File 'lib/cent/client.rb', line 117 def disconnect(user:) execute('disconnect', user: user) end |
#history(channel:) ⇒ Hash
Get channel history information
(list of last published into channel)
208 209 210 |
# File 'lib/cent/client.rb', line 208 def history(channel:) execute('history', channel: channel) end |
#info ⇒ Hash
Get information about running Centrifugo nodes
260 261 262 |
# File 'lib/cent/client.rb', line 260 def info execute('info', {}) end |
#presence(channel:) ⇒ Hash
Get channel presence information
(all clients currently subscribed on this channel)
149 150 151 |
# File 'lib/cent/client.rb', line 149 def presence(channel:) execute('presence', channel: channel) end |
#presence_stats(channel:) ⇒ Hash
Get short channel presence information
172 173 174 |
# File 'lib/cent/client.rb', line 172 def presence_stats(channel:) execute('presence_stats', channel: channel) end |
#publish(channel:, data:) ⇒ Hash
Publish data into channel
59 60 61 |
# File 'lib/cent/client.rb', line 59 def publish(channel:, data:) execute('publish', channel: channel, data: data) end |
#unsubscribe(channel:, user:) ⇒ Hash
Unsubscribe user from channel
99 100 101 |
# File 'lib/cent/client.rb', line 99 def unsubscribe(channel:, user:) execute('unsubscribe', channel: channel, user: user) end |