Connection
Connection Properties
The Connection
object exposes the following public properties:
id
A unique public identifier String
for this connection, used to identify this member in presence events and messages.
state
The current state String
of this Connection. See the Connection
states for more information.
errorReason
When a connection failure occurs this attribute contains the .
key
A unique private connection key String
used to recover or resume a connection, assigned by Ably. When recovering a connection explicitly, the recoveryKey
is used in the recover
client options as it contains both the key
and the last message serial
.
This private connection key can also be used by other REST clients to publish on behalf of this client. See the publishing over REST on behalf of a realtime client documentation for more info.
recoveryKey
The recovery key String
can be used by another client to recover this connection’s state in the recover
client options property. See connection state recover options for more information.
serial
The serial number Integer
of the last message to be received on this connection, used automatically by the library when recovering or resuming a connection. When recovering a connection explicitly, the recoveryKey
is used in the recover
client options as it contains both the key
and the last message serial
.
Connection Methods
connect
connect()
Explicitly calling connect
is unnecessary unless the ClientOptions
attribute autoConnect
is false
. Unless already connected
or connecting
, this method causes the connection to open, entering the connecting
state.
close
close()
Causes the connection to close, entering the closing
state. Once closed
, the library will not attempt to re-establish the connection without an explicit call to connect()
.
on
There are two overloaded versions of this method:
on(String event, listener(ConnectionStateChange stateChange))
Register the given listener for the specified ConnectionEvent
on the Connection
. The listener is passed a ConnectionStateChange object that contains the current state, previous state, and an optional reason for the event or state change.
on(listener(ConnectionStateChange stateChange))
Register the given listener for all ConnectionEvents on the Connection
. The listener is passed a ConnectionStateChange object that contains the current state, previous state, the event, and an optional reason for the event or state change. (For the update
event, the current and previous states will be the same).
Parameters
once
There are two overloaded versions of this method:
once(String event, listener(ConnectionStateChange stateChange))
Register the given listener for a single occurrence of the specified ConnectionEvent on the Connection
. Once the listener has been called, it is removed as a registered listener and will not be called again. The listener is passed a ConnectionStateChange object that contains the current state, previous state, the event, and an optional reason for the state change. (For the update
event, the current and previous states will be the same).
once(listener(ConnectionStateChange stateChange))
Register the given listener for a single occurrence of any ConnectionEvent on the Connection
. Once the listener has been called, it is removed as a registered listener and will not be called again. The listener is passed a ConnectionStateChange object that contains the current state, previous state, and an optional reason for the state change. (For the update
event, the current and previous states will be the same).
Parameters
off
There are two overloaded versions of this method:
off(String event, listener)
Remove the given listener for the ConnectionEvent.
off(listener)
Remove the given listener for all ConnectionEvents
Parameters
ping
ping(callback(ErrorInfo err, Number responseInMilliseconds))
When connected, sends a heartbeat ping to the Ably server and executes the callback when a heartbeat ping request is echoed from the server. This can be useful for measuring true round-trip latency to the connected Ably server.
Related types
ConnectionState
ConnectionEvent
ConnectionStateChange Object
A ConnectionStateChange
is a type encapsulating state change information emitted by the Connection
object. See Connection#on
to register a listener for one or more events.
Properties
- current
- the new stateType: State
String
- previous
- the previous state. (for the
update
event, this will be equal to thecurrent
state)Type: StateString
- event
- the event that triggered this state changeType:
ConnectionEvent
- reason
- an
ErrorInfo
containing any information relating to the transitionType:ErrorInfo
- retryIn
- Duration upon which the library will retry a connection where applicable, as millisecondsType:
Integer