Class: Cent::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cent/client.rb

Overview

Cent::Client

Main object that handles configuration and requests to centrifugo API

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, endpoint: 'http://localhost:8000/api') {|Faraday::Connection| ... } ⇒ Client

Returns a new instance of Client.

Examples:

Construct new client instance

Cent::Client.new(
  endpoint: 'http://localhost:8000/api',
  api_key: 'api key'
)

Parameters:

  • endpoint (String) (defaults to: 'http://localhost:8000/api')

    (default: ‘localhost:8000/api’) Centrifugo HTTP API URL

  • api_key (String)

    Centrifugo API key(used to perform requests)

Yields:

  • (Faraday::Connection)

    yields connection object so that it can be configured



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)

Examples:

Broadcast ‘content: ’hello’‘ into `channel_1`, ’channel_2’ channels

client.broadcast(channels: ['channel_1', 'channel_2'], data: 'hello') #=> {}

Parameters:

  • channels (Array<String>)

    Collection of channels names to publish

  • data (Hash)

    Data for publication in the channels

Returns:

  • (Hash)

    Return empty hash in case of successful broadcast

Raises:

See Also:



78
79
80
# File 'lib/cent/client.rb', line 78

def broadcast(channels:, data:)
  execute('broadcast', channels: channels, data: data)
end

#channelsHash

Get list of active(with one or more subscribers) channels.

Examples:

Get active channels list

client.channels #=> {
  "result" => {
    "channels" => [
      "chat"
    ]
  }
}

Returns:

  • (Hash)

    Return hash with a list of active channels

Raises:

See Also:



230
231
232
# File 'lib/cent/client.rb', line 230

def channels
  execute('channels', {})
end

#disconnect(user:) ⇒ Hash

Disconnect user by it’s ID

Examples:

Disconnect user with ‘id = 1`

client.disconnect(user: '1') #=> {}

Parameters:

  • user (String, Integer)

    User ID you want to disconnect

Returns:

  • (Hash)

    Return empty hash in case of successful disconnect

Raises:

See Also:



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 messages published into channel)

Examples:

Get history for channel ‘chat`

client.history(channel: 'chat') #=> {
  "result" => {
    "publications" => [
      {
        "data" => {
          "text" => "hello"
        },
        "uid" => "BWcn14OTBrqUhTXyjNg0fg"
      },
      {
        "data" => {
          "text" => "hi!"
        },
        "uid" => "Ascn14OTBrq14OXyjNg0hg"
      }
    ]
  }
}

Parameters:

  • channel (String)

    Name of the channel

Returns:

  • (Hash)

    Return hash with a list of last messages published into channel

Raises:

See Also:



208
209
210
# File 'lib/cent/client.rb', line 208

def history(channel:)
  execute('history', channel: channel)
end

#infoHash

Get information about running Centrifugo nodes

Examples:

Get running centrifugo nodes list

client.info #=> {
  "result" => {
    "nodes" => [
      {
        "name" => "Alexanders-MacBook-Pro.local_8000",
        "num_channels" => 0,
        "num_clients" => 0,
        "num_users" => 0,
        "uid" => "f844a2ed-5edf-4815-b83c-271974003db9",
        "uptime" => 0,
        "version" => ""
      }
    ]
  }
}

Returns:

  • (Hash)

    Return hash with a list of last messages published into channel

Raises:

See Also:



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)

Examples:

Get presence information for channel ‘chat`

client.presence(channel: 'chat') #=> {
  "result" => {
    "presence" => {
      "c54313b2-0442-499a-a70c-051f8588020f" => {
        "client" => "c54313b2-0442-499a-a70c-051f8588020f",
        "user" => "42"
      },
      "adad13b1-0442-499a-a70c-051f858802da" => {
        "client" => "adad13b1-0442-499a-a70c-051f858802da",
        "user" => "42"
      }
    }
  }
}

Parameters:

  • channel (String)

    Name of the channel

Returns:

  • (Hash)

    Return hash with information about all clients currently subscribed on this channel

Raises:

See Also:



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

Examples:

Get short presence information for channel ‘chat`

client.presence_stats(channel: 'chat') #=> {
  "result" => {
    "num_clients" => 0,
    "num_users" => 0
  }
}

Parameters:

  • channel (String)

    Name of the channel

Returns:

  • (Hash)

    Return hash with short presence information about channel

Raises:

See Also:



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

Examples:

Publish ‘content: ’hello’‘ into `chat` channel

client.publish(channel: 'chat', data: 'hello') #=> {}

Parameters:

  • channel (String)

    Name of the channel to publish

  • data (Hash)

    Data for publication in the channel

Returns:

  • (Hash)

    Return empty hash in case of successful publish

Raises:

See Also:



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

Examples:

Unsubscribe user with ‘id = 1` from `chat` channel

client.unsubscribe(channel: 'chat', user: '1') #=> {}

Parameters:

  • channel (String)

    Channel name to unsubscribe from

  • user (String, Integer)

    User ID you want to unsubscribe

Returns:

  • (Hash)

    Return empty hash in case of successful unsubscribe

Raises:

See Also:



99
100
101
# File 'lib/cent/client.rb', line 99

def unsubscribe(channel:, user:)
  execute('unsubscribe', channel: channel, user: user)
end