Class: Ably::Realtime::Client
- Inherits:
-
Object
- Object
- Ably::Realtime::Client
- 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
-
#auth ⇒ Ably::Auth
readonly
Authentication object configured for this connection.
-
#auth_options ⇒ Hash
readonly
Auth options configured for this client.
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#client_id ⇒ String
readonly
A client ID, used for identifying this client for presence purposes.
-
#environment ⇒ String
readonly
May contain ‘sandbox’ when testing the client library against an alternate Ably environment.
-
#tls ⇒ Boolean
readonly
True if client is configured to use TLS for all Ably communication.
Instance Method Summary collapse
- #attach_to_channel(channel_name) ⇒ Object
-
#channel(name) ⇒ Ably::Realtime::Channel
Return a Realtime Channel for the given name.
- #connection ⇒ Object
-
#debug_http? ⇒ Boolean
When true, will send HTTP & websocket debugging information for all messages/requests sent and received to STDOUT.
-
#endpoint ⇒ URI::Generic
Default Ably Realtime endpoint used for all requests.
-
#initialize(options) {|options| ... } ⇒ Ably::Realtime::Client
constructor
Creates a Realtime Client and configures the Auth object for the connection.
- #log_http(message) ⇒ Object
- #send_messages(channel_name, messages) ⇒ Object
- #token ⇒ Object
Methods included from Callbacks
Constructor Details
#initialize(options) {|options| ... } ⇒ Ably::Realtime::Client
Creates a Realtime Client and configures the Auth object for the connection.
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() @rest_client = Ably::Rest::Client.new() @auth = @rest_client.auth = 0 on(:attached) do || channel = channel(.channel) channel.trigger(:attached) end on(:message) do || channel = channel(.channel) ..each do || channel.trigger(:message, ) end end end |
Instance Attribute Details
#auth ⇒ Ably::Auth (readonly)
Returns 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() @rest_client = Ably::Rest::Client.new() @auth = @rest_client.auth = 0 on(:attached) do || channel = channel(.channel) channel.trigger(:attached) end on(:message) do || channel = channel(.channel) ..each do || channel.trigger(: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 (channel_name, ) payload = { action: Models::ProtocolMessage.action!(:message), channel: channel_name, 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() $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http? end private attr_reader :rest_client end |
#auth_options ⇒ Hash (readonly)
Returns 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() @rest_client = Ably::Rest::Client.new() @auth = @rest_client.auth = 0 on(:attached) do || channel = channel(.channel) channel.trigger(:attached) end on(:message) do || channel = channel(.channel) ..each do || channel.trigger(: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 (channel_name, ) payload = { action: Models::ProtocolMessage.action!(:message), channel: channel_name, 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() $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http? end private attr_reader :rest_client end |
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
21 22 23 |
# File 'lib/ably/realtime/client.rb', line 21 def channels @channels end |
#client_id ⇒ String (readonly)
Returns 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() @rest_client = Ably::Rest::Client.new() @auth = @rest_client.auth = 0 on(:attached) do || channel = channel(.channel) channel.trigger(:attached) end on(:message) do || channel = channel(.channel) ..each do || channel.trigger(: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 (channel_name, ) payload = { action: Models::ProtocolMessage.action!(:message), channel: channel_name, 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() $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http? end private attr_reader :rest_client end |
#environment ⇒ String (readonly)
Returns 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() @rest_client = Ably::Rest::Client.new() @auth = @rest_client.auth = 0 on(:attached) do || channel = channel(.channel) channel.trigger(:attached) end on(:message) do || channel = channel(.channel) ..each do || channel.trigger(: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 (channel_name, ) payload = { action: Models::ProtocolMessage.action!(:message), channel: channel_name, 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() $stdout.puts "#{Time.now.strftime('%H:%M:%S')} #{message}" if debug_http? end private attr_reader :rest_client end |
#tls ⇒ Boolean (readonly)
Returns 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() @rest_client = Ably::Rest::Client.new() @auth = @rest_client.auth = 0 on(:attached) do || channel = channel(.channel) channel.trigger(:attached) end on(:message) do || channel = channel(.channel) ..each do || channel.trigger(: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 (channel_name, ) payload = { action: Models::ProtocolMessage.action!(:message), channel: channel_name, 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() $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
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 |
#connection ⇒ Object
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
123 124 125 |
# File 'lib/ably/realtime/client.rb', line 123 def debug_http? rest_client.debug_http? end |
#endpoint ⇒ URI::Generic
Default Ably Realtime endpoint used for all requests
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() $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 (channel_name, ) payload = { action: Models::ProtocolMessage.action!(:message), channel: channel_name, messages: } payload.merge!(clientId: client_id) unless client_id.nil? connection.send(payload) end |
#token ⇒ Object
67 68 69 |
# File 'lib/ably/realtime/client.rb', line 67 def token @token ||= rest_client.request_token end |