Class: Ably::Realtime::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Callbacks
Defined in:
lib/ably/realtime/client.rb

Overview

Client for the Ably Realtime API

Constant Summary collapse

DOMAIN =
'realtime.ably.io'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#on, #trigger

Constructor Details

#initialize(options) {|options| ... } ⇒ Ably::Realtime::Client

Creates a Realtime Client and configures the Auth object for the connection.

Examples:

# create a new client authenticating with basic auth
client = Ably::Realtime::Client.new('key.id:secret')

# create a new client and configure a client ID used for presence
client = Ably::Realtime::Client.new(api_key: 'key.id:secret', client_id: 'john')

Parameters:

  • options (Hash, String)

    an options Hash used to configure the client and the authentication, or String with an API key

Options Hash (options):

  • :queue_messages (Boolean)

    If false, this disables the default behaviour whereby the library queues messages on a connection in the disconnected or connecting states

  • :echo_messages (Boolean)

    If false, prevents messages originating from this connection being echoed back on the same connection

  • :recover (String)

    This option allows a connection to inherit the state of a previous connection that may have existed under an different instance of the Realtime library.

  • :debug_http (Boolean)

    Send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT

  • :tls (Boolean)

    TLS is used by default, providing a value of false disbles TLS. Please note Basic Auth is disallowed without TLS as secrets cannot be transmitted over unsecured connections.

  • :api_key (String)

    API key comprising the key ID and key secret in a single string

  • :environment (String)

    Specify ‘sandbox’ when testing the client library against an alternate Ably environment

  • :debug_http (Boolean)

    Send HTTP debugging information from Faraday for all HTTP requests to STDOUT

  • :key_id (String)

    key ID for the designated application (defaults to client key_id)

  • :key_secret (String)

    key secret for the designated application used to sign token requests (defaults to client key_secret)

  • :client_id (String)

    client ID identifying this connection to other clients (defaults to client client_id if configured)

  • :auth_url (String)

    a URL to be used to GET or POST a set of token request params, to obtain a signed token request.

  • :auth_headers (Hash)

    a set of application-specific headers to be added to any request made to the authUrl

  • :auth_params (Hash)

    a set of application-specific query params to be added to any request made to the authUrl

  • :auth_method (Symbol)

    HTTP method to use with auth_url, must be either :get or :post (defaults to :get)

  • :ttl (Integer)

    validity time in seconds for the requested Token. Limits may apply, see /

  • :capability (Hash)

    canonicalised representation of the resource paths and associated operations

  • :query_time (Boolean)

    when true will query the Ably system for the current time instead of using the local time

  • :timestamp (Time)

    the time of the of the request

  • :nonce (String)

    an unquoted, unescaped random string of at least 16 characters

  • :force (Boolean)

    obtains a new token even if the current token is valid

Yields:

  • (options)

    (optional) if an auth block is passed to this method, then this block will be called to create a new token request object

Yield Parameters:

  • options (Hash)

    options passed to request_token will be in turn sent to the block in this argument

Yield Returns:

  • (Hash)

    valid token request object, see #create_token_request



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ably/realtime/client.rb', line 47

def initialize(options)
  @rest_client    = Ably::Rest::Client.new(options)
  @auth           = @rest_client.auth
  @message_serial = 0

  on(:attached) do |protocol_message|
    channel = channel(protocol_message.channel)

    channel.trigger(:attached)
  end

  on(:message) do |protocol_message|
    channel = channel(protocol_message.channel)

    protocol_message.messages.each do |message|
      channel.trigger(:message, message)
    end
  end
end

Instance Attribute Details

#authAbly::Auth (readonly)

Returns authentication object configured for this connection.

Returns:

  • (Ably::Auth)

    authentication object configured for this connection



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ably/realtime/client.rb', line 15

class Client
  include Callbacks
  extend Forwardable

  DOMAIN = 'realtime.ably.io'

  attr_reader :channels, :auth
  def_delegators :auth, :client_id, :auth_options
  def_delegators :@rest_client, :tls, :environment, :use_tls?

  # Creates a {Ably::Realtime::Client Realtime Client} and configures the {Ably::Auth} object for the connection.
  #
  # @param (see Ably::Rest::Client#initialize)
  # @option options (see Ably::Rest::Client#initialize)
  # @option options [Boolean] :queue_messages If false, this disables the default behaviour whereby the library queues messages on a connection in the disconnected or connecting states
  # @option options [Boolean] :echo_messages  If false, prevents messages originating from this connection being echoed back on the same connection
  # @option options [String]  :recover        This option allows a connection to inherit the state of a previous connection that may have existed under an different instance of the Realtime library.
  # @option options [Boolean] :debug_http     Send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @yield (see Ably::Rest::Client#initialize)
  # @yieldparam (see Ably::Rest::Client#initialize)
  # @yieldreturn (see Ably::Rest::Client#initialize)
  #
  # @return [Ably::Realtime::Client]
  #
  # @example
  #    # create a new client authenticating with basic auth
  #    client = Ably::Realtime::Client.new('key.id:secret')
  #
  #    # create a new client and configure a client ID used for presence
  #    client = Ably::Realtime::Client.new(api_key: 'key.id:secret', client_id: 'john')
  #
  def initialize(options)
    @rest_client    = Ably::Rest::Client.new(options)
    @auth           = @rest_client.auth
    @message_serial = 0

    on(:attached) do |protocol_message|
      channel = channel(protocol_message.channel)

      channel.trigger(:attached)
    end

    on(:message) do |protocol_message|
      channel = channel(protocol_message.channel)

      protocol_message.messages.each do |message|
        channel.trigger(:message, message)
      end
    end
  end

  def token
    @token ||= rest_client.request_token
  end

  # Return a Realtime Channel for the given name
  #
  # @param name [String] The name of the channel
  # @return [Ably::Realtime::Channel]
  def channel(name)
    @channels ||= {}
    @channels[name] ||= Ably::Realtime::Channel.new(self, name)
  end

  def send_messages(channel_name, messages)
    payload = {
      action:   Models::ProtocolMessage.action!(:message),
      channel:  channel_name,
      messages: messages
    }

    payload.merge!(clientId: client_id) unless client_id.nil?

    connection.send(payload)
  end

  def attach_to_channel(channel_name)
    payload = {
      action:  Models::ProtocolMessage.action!(:attach),
      channel: channel_name
    }

    connection.send(payload)
  end

  # Default Ably Realtime endpoint used for all requests
  #
  # @return [URI::Generic]
  def endpoint
    URI::Generic.build(
      scheme: use_tls? ? "wss" : "ws",
      host:   [environment, DOMAIN].compact.join('-')
    )
  end

  def connection
    @connection ||= begin
      host = endpoint.host
      port = use_tls? ? 443 : 80

      EventMachine.connect(host, port, Connection, self)
    end
  end

  # When true, will send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @return [Boolean]
  def debug_http?
    rest_client.debug_http?
  end

  def log_http(message)
    $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http?
  end

  private
  attr_reader :rest_client
end

#auth_optionsHash (readonly)

Returns Auth options configured for this client.

Returns:

  • (Hash)

    Auth options configured for this client



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ably/realtime/client.rb', line 15

class Client
  include Callbacks
  extend Forwardable

  DOMAIN = 'realtime.ably.io'

  attr_reader :channels, :auth
  def_delegators :auth, :client_id, :auth_options
  def_delegators :@rest_client, :tls, :environment, :use_tls?

  # Creates a {Ably::Realtime::Client Realtime Client} and configures the {Ably::Auth} object for the connection.
  #
  # @param (see Ably::Rest::Client#initialize)
  # @option options (see Ably::Rest::Client#initialize)
  # @option options [Boolean] :queue_messages If false, this disables the default behaviour whereby the library queues messages on a connection in the disconnected or connecting states
  # @option options [Boolean] :echo_messages  If false, prevents messages originating from this connection being echoed back on the same connection
  # @option options [String]  :recover        This option allows a connection to inherit the state of a previous connection that may have existed under an different instance of the Realtime library.
  # @option options [Boolean] :debug_http     Send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @yield (see Ably::Rest::Client#initialize)
  # @yieldparam (see Ably::Rest::Client#initialize)
  # @yieldreturn (see Ably::Rest::Client#initialize)
  #
  # @return [Ably::Realtime::Client]
  #
  # @example
  #    # create a new client authenticating with basic auth
  #    client = Ably::Realtime::Client.new('key.id:secret')
  #
  #    # create a new client and configure a client ID used for presence
  #    client = Ably::Realtime::Client.new(api_key: 'key.id:secret', client_id: 'john')
  #
  def initialize(options)
    @rest_client    = Ably::Rest::Client.new(options)
    @auth           = @rest_client.auth
    @message_serial = 0

    on(:attached) do |protocol_message|
      channel = channel(protocol_message.channel)

      channel.trigger(:attached)
    end

    on(:message) do |protocol_message|
      channel = channel(protocol_message.channel)

      protocol_message.messages.each do |message|
        channel.trigger(:message, message)
      end
    end
  end

  def token
    @token ||= rest_client.request_token
  end

  # Return a Realtime Channel for the given name
  #
  # @param name [String] The name of the channel
  # @return [Ably::Realtime::Channel]
  def channel(name)
    @channels ||= {}
    @channels[name] ||= Ably::Realtime::Channel.new(self, name)
  end

  def send_messages(channel_name, messages)
    payload = {
      action:   Models::ProtocolMessage.action!(:message),
      channel:  channel_name,
      messages: messages
    }

    payload.merge!(clientId: client_id) unless client_id.nil?

    connection.send(payload)
  end

  def attach_to_channel(channel_name)
    payload = {
      action:  Models::ProtocolMessage.action!(:attach),
      channel: channel_name
    }

    connection.send(payload)
  end

  # Default Ably Realtime endpoint used for all requests
  #
  # @return [URI::Generic]
  def endpoint
    URI::Generic.build(
      scheme: use_tls? ? "wss" : "ws",
      host:   [environment, DOMAIN].compact.join('-')
    )
  end

  def connection
    @connection ||= begin
      host = endpoint.host
      port = use_tls? ? 443 : 80

      EventMachine.connect(host, port, Connection, self)
    end
  end

  # When true, will send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @return [Boolean]
  def debug_http?
    rest_client.debug_http?
  end

  def log_http(message)
    $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http?
  end

  private
  attr_reader :rest_client
end

#channelsObject (readonly)

Returns the value of attribute channels.



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

def channels
  @channels
end

#client_idString (readonly)

Returns A client ID, used for identifying this client for presence purposes.

Returns:

  • (String)

    A client ID, used for identifying this client for presence purposes



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ably/realtime/client.rb', line 15

class Client
  include Callbacks
  extend Forwardable

  DOMAIN = 'realtime.ably.io'

  attr_reader :channels, :auth
  def_delegators :auth, :client_id, :auth_options
  def_delegators :@rest_client, :tls, :environment, :use_tls?

  # Creates a {Ably::Realtime::Client Realtime Client} and configures the {Ably::Auth} object for the connection.
  #
  # @param (see Ably::Rest::Client#initialize)
  # @option options (see Ably::Rest::Client#initialize)
  # @option options [Boolean] :queue_messages If false, this disables the default behaviour whereby the library queues messages on a connection in the disconnected or connecting states
  # @option options [Boolean] :echo_messages  If false, prevents messages originating from this connection being echoed back on the same connection
  # @option options [String]  :recover        This option allows a connection to inherit the state of a previous connection that may have existed under an different instance of the Realtime library.
  # @option options [Boolean] :debug_http     Send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @yield (see Ably::Rest::Client#initialize)
  # @yieldparam (see Ably::Rest::Client#initialize)
  # @yieldreturn (see Ably::Rest::Client#initialize)
  #
  # @return [Ably::Realtime::Client]
  #
  # @example
  #    # create a new client authenticating with basic auth
  #    client = Ably::Realtime::Client.new('key.id:secret')
  #
  #    # create a new client and configure a client ID used for presence
  #    client = Ably::Realtime::Client.new(api_key: 'key.id:secret', client_id: 'john')
  #
  def initialize(options)
    @rest_client    = Ably::Rest::Client.new(options)
    @auth           = @rest_client.auth
    @message_serial = 0

    on(:attached) do |protocol_message|
      channel = channel(protocol_message.channel)

      channel.trigger(:attached)
    end

    on(:message) do |protocol_message|
      channel = channel(protocol_message.channel)

      protocol_message.messages.each do |message|
        channel.trigger(:message, message)
      end
    end
  end

  def token
    @token ||= rest_client.request_token
  end

  # Return a Realtime Channel for the given name
  #
  # @param name [String] The name of the channel
  # @return [Ably::Realtime::Channel]
  def channel(name)
    @channels ||= {}
    @channels[name] ||= Ably::Realtime::Channel.new(self, name)
  end

  def send_messages(channel_name, messages)
    payload = {
      action:   Models::ProtocolMessage.action!(:message),
      channel:  channel_name,
      messages: messages
    }

    payload.merge!(clientId: client_id) unless client_id.nil?

    connection.send(payload)
  end

  def attach_to_channel(channel_name)
    payload = {
      action:  Models::ProtocolMessage.action!(:attach),
      channel: channel_name
    }

    connection.send(payload)
  end

  # Default Ably Realtime endpoint used for all requests
  #
  # @return [URI::Generic]
  def endpoint
    URI::Generic.build(
      scheme: use_tls? ? "wss" : "ws",
      host:   [environment, DOMAIN].compact.join('-')
    )
  end

  def connection
    @connection ||= begin
      host = endpoint.host
      port = use_tls? ? 443 : 80

      EventMachine.connect(host, port, Connection, self)
    end
  end

  # When true, will send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @return [Boolean]
  def debug_http?
    rest_client.debug_http?
  end

  def log_http(message)
    $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http?
  end

  private
  attr_reader :rest_client
end

#environmentString (readonly)

Returns May contain ‘sandbox’ when testing the client library against an alternate Ably environment.

Returns:

  • (String)

    May contain ‘sandbox’ when testing the client library against an alternate Ably environment



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ably/realtime/client.rb', line 15

class Client
  include Callbacks
  extend Forwardable

  DOMAIN = 'realtime.ably.io'

  attr_reader :channels, :auth
  def_delegators :auth, :client_id, :auth_options
  def_delegators :@rest_client, :tls, :environment, :use_tls?

  # Creates a {Ably::Realtime::Client Realtime Client} and configures the {Ably::Auth} object for the connection.
  #
  # @param (see Ably::Rest::Client#initialize)
  # @option options (see Ably::Rest::Client#initialize)
  # @option options [Boolean] :queue_messages If false, this disables the default behaviour whereby the library queues messages on a connection in the disconnected or connecting states
  # @option options [Boolean] :echo_messages  If false, prevents messages originating from this connection being echoed back on the same connection
  # @option options [String]  :recover        This option allows a connection to inherit the state of a previous connection that may have existed under an different instance of the Realtime library.
  # @option options [Boolean] :debug_http     Send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @yield (see Ably::Rest::Client#initialize)
  # @yieldparam (see Ably::Rest::Client#initialize)
  # @yieldreturn (see Ably::Rest::Client#initialize)
  #
  # @return [Ably::Realtime::Client]
  #
  # @example
  #    # create a new client authenticating with basic auth
  #    client = Ably::Realtime::Client.new('key.id:secret')
  #
  #    # create a new client and configure a client ID used for presence
  #    client = Ably::Realtime::Client.new(api_key: 'key.id:secret', client_id: 'john')
  #
  def initialize(options)
    @rest_client    = Ably::Rest::Client.new(options)
    @auth           = @rest_client.auth
    @message_serial = 0

    on(:attached) do |protocol_message|
      channel = channel(protocol_message.channel)

      channel.trigger(:attached)
    end

    on(:message) do |protocol_message|
      channel = channel(protocol_message.channel)

      protocol_message.messages.each do |message|
        channel.trigger(:message, message)
      end
    end
  end

  def token
    @token ||= rest_client.request_token
  end

  # Return a Realtime Channel for the given name
  #
  # @param name [String] The name of the channel
  # @return [Ably::Realtime::Channel]
  def channel(name)
    @channels ||= {}
    @channels[name] ||= Ably::Realtime::Channel.new(self, name)
  end

  def send_messages(channel_name, messages)
    payload = {
      action:   Models::ProtocolMessage.action!(:message),
      channel:  channel_name,
      messages: messages
    }

    payload.merge!(clientId: client_id) unless client_id.nil?

    connection.send(payload)
  end

  def attach_to_channel(channel_name)
    payload = {
      action:  Models::ProtocolMessage.action!(:attach),
      channel: channel_name
    }

    connection.send(payload)
  end

  # Default Ably Realtime endpoint used for all requests
  #
  # @return [URI::Generic]
  def endpoint
    URI::Generic.build(
      scheme: use_tls? ? "wss" : "ws",
      host:   [environment, DOMAIN].compact.join('-')
    )
  end

  def connection
    @connection ||= begin
      host = endpoint.host
      port = use_tls? ? 443 : 80

      EventMachine.connect(host, port, Connection, self)
    end
  end

  # When true, will send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @return [Boolean]
  def debug_http?
    rest_client.debug_http?
  end

  def log_http(message)
    $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http?
  end

  private
  attr_reader :rest_client
end

#tlsBoolean (readonly)

Returns True if client is configured to use TLS for all Ably communication.

Returns:

  • (Boolean)

    True if client is configured to use TLS for all Ably communication



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ably/realtime/client.rb', line 15

class Client
  include Callbacks
  extend Forwardable

  DOMAIN = 'realtime.ably.io'

  attr_reader :channels, :auth
  def_delegators :auth, :client_id, :auth_options
  def_delegators :@rest_client, :tls, :environment, :use_tls?

  # Creates a {Ably::Realtime::Client Realtime Client} and configures the {Ably::Auth} object for the connection.
  #
  # @param (see Ably::Rest::Client#initialize)
  # @option options (see Ably::Rest::Client#initialize)
  # @option options [Boolean] :queue_messages If false, this disables the default behaviour whereby the library queues messages on a connection in the disconnected or connecting states
  # @option options [Boolean] :echo_messages  If false, prevents messages originating from this connection being echoed back on the same connection
  # @option options [String]  :recover        This option allows a connection to inherit the state of a previous connection that may have existed under an different instance of the Realtime library.
  # @option options [Boolean] :debug_http     Send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @yield (see Ably::Rest::Client#initialize)
  # @yieldparam (see Ably::Rest::Client#initialize)
  # @yieldreturn (see Ably::Rest::Client#initialize)
  #
  # @return [Ably::Realtime::Client]
  #
  # @example
  #    # create a new client authenticating with basic auth
  #    client = Ably::Realtime::Client.new('key.id:secret')
  #
  #    # create a new client and configure a client ID used for presence
  #    client = Ably::Realtime::Client.new(api_key: 'key.id:secret', client_id: 'john')
  #
  def initialize(options)
    @rest_client    = Ably::Rest::Client.new(options)
    @auth           = @rest_client.auth
    @message_serial = 0

    on(:attached) do |protocol_message|
      channel = channel(protocol_message.channel)

      channel.trigger(:attached)
    end

    on(:message) do |protocol_message|
      channel = channel(protocol_message.channel)

      protocol_message.messages.each do |message|
        channel.trigger(:message, message)
      end
    end
  end

  def token
    @token ||= rest_client.request_token
  end

  # Return a Realtime Channel for the given name
  #
  # @param name [String] The name of the channel
  # @return [Ably::Realtime::Channel]
  def channel(name)
    @channels ||= {}
    @channels[name] ||= Ably::Realtime::Channel.new(self, name)
  end

  def send_messages(channel_name, messages)
    payload = {
      action:   Models::ProtocolMessage.action!(:message),
      channel:  channel_name,
      messages: messages
    }

    payload.merge!(clientId: client_id) unless client_id.nil?

    connection.send(payload)
  end

  def attach_to_channel(channel_name)
    payload = {
      action:  Models::ProtocolMessage.action!(:attach),
      channel: channel_name
    }

    connection.send(payload)
  end

  # Default Ably Realtime endpoint used for all requests
  #
  # @return [URI::Generic]
  def endpoint
    URI::Generic.build(
      scheme: use_tls? ? "wss" : "ws",
      host:   [environment, DOMAIN].compact.join('-')
    )
  end

  def connection
    @connection ||= begin
      host = endpoint.host
      port = use_tls? ? 443 : 80

      EventMachine.connect(host, port, Connection, self)
    end
  end

  # When true, will send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT
  #
  # @return [Boolean]
  def debug_http?
    rest_client.debug_http?
  end

  def log_http(message)
    $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http?
  end

  private
  attr_reader :rest_client
end

Instance Method Details

#attach_to_channel(channel_name) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/ably/realtime/client.rb', line 92

def attach_to_channel(channel_name)
  payload = {
    action:  Models::ProtocolMessage.action!(:attach),
    channel: channel_name
  }

  connection.send(payload)
end

#channel(name) ⇒ Ably::Realtime::Channel

Return a Realtime Channel for the given name

Parameters:

  • name (String)

    The name of the channel

Returns:



75
76
77
78
# File 'lib/ably/realtime/client.rb', line 75

def channel(name)
  @channels ||= {}
  @channels[name] ||= Ably::Realtime::Channel.new(self, name)
end

#connectionObject



111
112
113
114
115
116
117
118
# File 'lib/ably/realtime/client.rb', line 111

def connection
  @connection ||= begin
    host = endpoint.host
    port = use_tls? ? 443 : 80

    EventMachine.connect(host, port, Connection, self)
  end
end

#debug_http?Boolean

When true, will send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT

Returns:

  • (Boolean)


123
124
125
# File 'lib/ably/realtime/client.rb', line 123

def debug_http?
  rest_client.debug_http?
end

#endpointURI::Generic

Default Ably Realtime endpoint used for all requests

Returns:

  • (URI::Generic)


104
105
106
107
108
109
# File 'lib/ably/realtime/client.rb', line 104

def endpoint
  URI::Generic.build(
    scheme: use_tls? ? "wss" : "ws",
    host:   [environment, DOMAIN].compact.join('-')
  )
end

#log_http(message) ⇒ Object



127
128
129
# File 'lib/ably/realtime/client.rb', line 127

def log_http(message)
  $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http?
end

#send_messages(channel_name, messages) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ably/realtime/client.rb', line 80

def send_messages(channel_name, messages)
  payload = {
    action:   Models::ProtocolMessage.action!(:message),
    channel:  channel_name,
    messages: messages
  }

  payload.merge!(clientId: client_id) unless client_id.nil?

  connection.send(payload)
end

#tokenObject



67
68
69
# File 'lib/ably/realtime/client.rb', line 67

def token
  @token ||= rest_client.request_token
end