Class: Rubycent::Client

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

Overview

Rubycent::Client

Main object that handles configuration and requests to centrifugo API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Examples:

Construct new client instance

Rubycent::Client.new(
  scheme: 'http',
  host: 'localhost',
  port: '8000',
  secret: 'secret',
  api_key: 'api key',
  timeout: 10,
  open_timeout: 15
)

Parameters:

  • options (Hash) (defaults to: {})

    (default: {}) Parameters to configure centrifugo client

Options Hash (options):

  • :scheme (String)

    Centrifugo address scheme

  • :host (String)

    Centrifugo address host

  • :port (String)

    Centrifugo address port

  • :secret (String)

    Centrifugo secret(used to issue JWT)

  • :api_key (String)

    Centrifugo API key(used to perform requests)

  • :timeout (String)

    Number of seconds to wait for the connection to open.

  • :open_timeout (String)

    Number of seconds to wait for one block to be read.



60
61
62
63
64
65
66
67
68
69
# File 'lib/rubycent/client.rb', line 60

def initialize(options = {})
  options = DEFAULT_OPTIONS.merge(options)

  @scheme, @host, @port, @secret, @api_key = options.values_at(
    :scheme, :host, :port, :secret, :api_key
  )

  @timeout = 5
  @open_timeout = 5
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



21
22
23
# File 'lib/rubycent/client.rb', line 21

def api_key
  @api_key
end

#hostObject

Returns the value of attribute host.



21
22
23
# File 'lib/rubycent/client.rb', line 21

def host
  @host
end

#open_timeoutObject

Returns the value of attribute open_timeout.



23
24
25
# File 'lib/rubycent/client.rb', line 23

def open_timeout
  @open_timeout
end

#portObject

Returns the value of attribute port.



21
22
23
# File 'lib/rubycent/client.rb', line 21

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



21
22
23
# File 'lib/rubycent/client.rb', line 21

def scheme
  @scheme
end

#secretObject

Returns the value of attribute secret.



21
22
23
# File 'lib/rubycent/client.rb', line 21

def secret
  @secret
end

#timeoutObject

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)

Examples:

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

Rubycent::Client.new.broadcast(['channel_1', 'channel_2'], content: '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:



107
108
109
# File 'lib/rubycent/client.rb', line 107

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

#channelsHash

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

Examples:

Get active channels list

Rubycent::Client.new.channels #=> {
  "result" => {
    "channels" => [
      "chat"
    ]
  }
}

Returns:

  • (Hash)

    Return hash with a list of active channels

Raises:

See Also:



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

Examples:

Disconnect user with ‘id = 1`

Rubycent::Client.new.disconnect(1) #=> {}

Parameters:

  • user_id (String, Integer)

    User ID you want to disconnect

Returns:

  • (Hash)

    Return empty hash in case of successful disconnect

Raises:

See Also:



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

Examples:

Get history for channel ‘chat`

Rubycent::Client.new.history('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:



237
238
239
# File 'lib/rubycent/client.rb', line 237

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

#infoHash

Get information about running Centrifugo nodes

Examples:

Get running centrifugo nodes list

Rubycent::Client.new.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:



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

Note:

At moment the only supported JWT algorithm is HS256 - i.e. HMAC SHA-256. This can be extended later.

Generate JWT for private channels

Examples:

Get private channel JWT with expiration and extra info

Rubycent::Client.new.issue_channel_token('client', 'channel', 3600, { 'message' => 'wat' }) #=> eyJhbGciOiJIUzI1NiJ9.eyJjbGllbnQiOiJjbG..."

Parameters:

  • client (String)

    Client ID which wants to subscribe on channel

  • expiration (Integer) (defaults to: nil)

    (default: nil) UNIX timestamp seconds when token will expire.

  • info (Hash) (defaults to: {})

    (default: {}) This claim is optional - this is additional information about client connection that can be provided for Centrifugo.

  • algorithm (String) (defaults to: 'HS256')

    The algorithm used for the cryptographic signing

  • channel (Hash)

    a customizable set of options

Options Hash (channel):

  • Channel (String)

    that client tries to subscribe to (string).

Returns:

  • (String)

Raises:

See Also:



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

Note:

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

Examples:

Get user JWT with expiration and extra info

Rubycent::Client.new.issue_user_token('1', 3600, { 'role' => 'admin' }) #=> "eyJhbGciOiJIUzI1NiJ9.eyJzdWIi..."

Parameters:

  • user_id (String)

    Standard JWT claim which must contain an ID of current application user.

  • expiration (Integer) (defaults to: nil)

    (default: nil) UNIX timestamp seconds when token will expire.

  • info (Hash) (defaults to: {})

    (default: {}) This claim is optional - this is additional information about client connection that can be provided for Centrifugo.

  • algorithm (String) (defaults to: 'HS256')

    The algorithm used for the cryptographic signing

  • subscriber (Hash)

    a customizable set of options

Returns:

  • (String)

Raises:

See Also:



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)

Examples:

Get presence information for channel ‘chat`

Rubycent::Client.new.presence('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:



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

Examples:

Get short presence information for channel ‘chat`

Rubycent::Client.new.presence_stats('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:



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

Examples:

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

Rubycent::Client.new.publish('chat', content: '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:



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

Examples:

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

Rubycent::Client.new.unsubscribe('chat', 1) #=> {}

Parameters:

  • channel (String)

    Channel name to unsubscribe from

  • user_id (String, Integer)

    User ID you want to unsubscribe

Returns:

  • (Hash)

    Return empty hash in case of successful unsubscribe

Raises:

See Also:



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