new RTM(endpoint, appkey, opts)
An RTM client is the main entry point for accessing RTM.
To connect a client to RTM, you must call RTM.start()
. The RTM SDK attempts
to reconnect to RTM if the connection to RTM fails for any reason.
A client instance can be in one of the following connection states:
- stopped: You called
RTM.stop()
or RTM disconnected and hasn't yet reconnected. - connecting: You called
RTM.start()
or the client is trying to reconnect to RTM. - connected: The client is connected to RTM.
- awaiting: The client disconnected and is waiting the specified time period
before reconnecting. See the
minReconnectInterval
andmaxReconnectInterval
options.
For each state, an event occurs when the client enters or leaves the state. Call
RTM.on(name, fn)
method to add code that's executed when the client
transitions into or out of a state. The syntax for the value of name
is
[ enter- | leave- ][ stopped | connecting | connected | awaiting ]
For example, RTM.on("enter-connected", myFunction)
. The next example also shows you
how to call RTM.on()
Properties:
Name | Type | Description |
---|---|---|
writable |
boolean
|
A general indicator of the status of the write buffer.
|
Parameters:
Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
endpoint |
string
|
WebSocket endpoint for RTM Available from the Dev Portal. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
appkey |
string
|
appkey used to access RTM Available from the Dev Portal. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
opts |
object
|
additional RTM client parameters
|
Throws:
-
TypeError
is thrown if mandatory parameters are missing or invalid. - Type
-
TypeError
Example
// Creates an RTM client
var rtm = new RTM('YOUR_ENDPOINT', 'YOUR_APPKEY');
// Creates a new subscription to the channel named 'your-channel'
var subscription = rtm.subscribe('your-channel', RTM.SubscriptionMode.SIMPLE);
// Adds a subscription event listener that logs messages to the console as they arrive.
// The subscription receives all messages in the subscribed channel
subscription.on('rtm/subscription/data', function (pdu) {
pdu.body.messages.forEach(console.log);
});
// Sets a connection event listener that publishes a message to the channel named
// <code>your-channel</code>
// when the client is connected to RTM (the client enters the 'connected' state)
rtm.on('enter-connected', function () {
rtm.publish('your-channel', {key: 'value'});
});
// Sets a client event listener that checks incoming messages to see if they indicate an error.
// <code>rtm.on()</code> is called for all incoming messages.
rtm.on('data', function (pdu) {
if (pdu.action.endsWith('/error')) {
rtm.restart();
}
});
// Starts the client
rtm.start();
Extends
Namespaces
Methods
(static) roleSecretAuthProvider(role, roleSecret, opts) → {function}
Creates a role-based authentication provider for the client
The role-based authentication method is a two-step authentication process based on the HMAC process, using the MD5 hashing routine:
- The client obtains a nonce from the server in a handshake request.
- The client then sends an authorization request with its role secret key hashed with the received nonce.
To get a role secret key for your application, go to the Dev Portal.
Parameters:
Name | Type | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
role |
string
|
role name set in the Dev Portal |
||||||||||
roleSecret |
string
|
role secret key |
||||||||||
opts |
object
|
additional authentication options
|
Throws:
-
thrown if mandatory parameters are missing or invalid
- Type
-
TypeError
Returns:
- Type:
-
function
authentication provider for the role-based authentication method
delete(channel, onAckopt) → {void}
Deletes the value for the associated channel. The RTM
client must be connected.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
channel |
string
|
Channel name. |
|
onAck |
function
|
<optional> |
Callback function that's invoked when RTM responds to the publish request. RTM passes the
response PDU to this function. If you don't specify |
Throws:
-
thrown if required parameters are missing or invalid
- Type
-
TypeError
Returns:
- Type:
-
void
Example
rtm.delete('channel', function (pdu) {
console.log(pdu);
})
fire(name, …args) → {void}
Executes all handlers attached for the specified event type.
The event specified in name
is an RTM
or
Subscription
event that has an attached event handler function
(see the on()
function).
- Inherited From:
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string
|
name of an event that has attached handlers |
|
args |
Object
|
<repeatable> |
event arguments. |
Returns:
- Type:
-
void
getSubscription(subscriptionId) → {Subscription}
Returns the existing Subscription
object for the specified subscription id.
Parameters:
Name | Type | Description |
---|---|---|
subscriptionId |
string
|
the id for an existing |
Throws:
-
thrown if
subscriptionId
is missing, invalid, or if aSubscription
object with that id doesn't exist. - Type
-
TypeError
isConnected() → {boolean}
Returns true
if the client is in the connected
state.
In this state, the WebSocket connection to RTM is established and any requested authentication has completed successfully .
Returns:
- Type:
-
boolean
true
if the client is in the connected
state,
otherwise false
isStopped() → {boolean}
Returns true
if the RTM client is in the stopped
state.
Returns:
- Type:
-
boolean
true
if the client is in the stopped
state,
otherwise false
off(name, fn) → {void}
Removes an event handler.
The event specified in name
is an RTM
or
Subscription
event that has an attached event handler function
(see the on()
function).
The Protocol Data Unit (PDU) for the event is passed to the
fn
function parameter.
- Inherited From:
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
event name |
fn |
function
|
event handler function |
Returns:
- Type:
-
void
on(name, fn) → {void}
Attaches an event handler function for the event specified in name
.
The event is usually related to a client or subscription state. It may also be an event
that occurs when the client or subscription receives information from RTM. For example, the
the following are RTM
client events:
data:
The client received a PDU from RTM.enter-connected:
The client is now connected to RTM.
Subscription
is enter-subscribed
.
The fn
parameter is a function that's invoked when the event occurs. The PDU for
the event is passed to this function.
- Inherited From:
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
event name |
fn |
function
|
event handler function |
Returns:
- Type:
-
void
publish(channel, message, onAckopt) → {void}
Publishes a message to a channel. The client must be connected.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
channel |
string
|
channel name |
|
message |
JSON
|
Uint8Array
|
|
|
onAck |
function
|
<optional> |
Callback function that's invoked when RTM responds to the publish request. RTM passes the
response PDU to this function. If you don't specify |
Throws:
-
thrown if required parameters are missing or invalid
- Type
-
TypeError
Returns:
- Type:
-
void
Example
// Publishes to the channel named "channel", and provides a callback function that's invoked when
// RTM responds to the request. If the PDU "action" value doesn't end with "ok", the function
// logs an error.
rtm.publish('channel', {key: 'value'}, function (pdu) {
if (!pdu.action.endsWith('/ok')) {
console.log('something went wrong');
}
});
read(channel, onAckOrOptsopt) → {void}
Reads the latest message written to a specific channel, as a Protocol Data Unit (PDU). The client must be connected.
The callback function you specify receives a PDU in the same format as the
subprotocol you specify in the client constructor RTM
. RTM automatically converts
messages.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
channel |
string
|
name of the channel to read from |
|
onAckOrOpts |
function
|
<optional> |
Callback function that's invoked when RTM responds to the read request. RTM passes the
response PDU to this function. If you don't specify |
Throws:
-
thrown if required parameters are missing or invalid
- Type
-
TypeError
Returns:
- Type:
-
void
Example
// Reads from the channel named 'channel' and prints the response PDU
rtm.read('channel', function (pdu) {
console.log(pdu);
})
read(channel, optsopt) → {void}
Reads the latest message written to specific channel, as a Protocol Data Unit (PDU). The client must be connected.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
channel |
string
|
name of the channel to read from |
|||||||||||||||||
opts |
object
|
<optional> |
{} |
Additional options in the read PDU that's sent to RTM in the request. For more information, see the section "Read PDU" in the "RTM API" chapter of Satori Docs/em>.
|
Throws:
-
thrown if required parameters are missing or invalid
- Type
-
TypeError
Returns:
- Type:
-
void
Example
// Reads from the channel named 'channel', starting at the position specified by the
// "position" key.
// Prints the response PDU.
rtm.read('channel', {
bodyOpts: { position: '1485444476:0' },
onAck: function (pdu) {
console.log(pdu);
}
})
resubscribe(channelOrSubId, mode, bodyOpts, onCompletedopt) → {void}
Updates an existing Subscription
object. Existing
Subscription
event handlers are copied to the updated object.
Use this method to change an existing subscription. For example, use it to add or change a streamview.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
channelOrSubId |
string
|
subscription id or channel name for the existing subscription |
|
mode |
RTM.SubscriptionMode
|
Contains flags that determine the resubscribe behavior of
the RTM SDK and RTM. See |
|
bodyOpts |
Object
|
Properties for the updated |
|
onCompleted |
function
|
<optional> |
function to execute on the updated |
Throws:
-
thrown if mandatory parameters are missing or invalid.
- Type
-
TypeError
Returns:
- Type:
-
void
start() → {void}
Starts the client.
The client begins to establish the WebSocket connection to RTM and then tracks the state of the connection. If the WebSocket connection drops for any reason, the JavaScript SDK attempts to reconnect.
Use [RTM.on(name, fn)]RTM.on()
to define application functionality,
for example, when the application enters or leaves the
connecting
or connected
states.
Returns:
- Type:
-
void
stop() → {void}
Stops the client. The RTM SDK starts to close the WebSocket connection and
does not start it again unless you call RTM.start()
.
Use this method to explicitly stop all interaction with RTM.
Use RTM.on("enter-stopped", function())
or
RTM.on("leave-stopped", function())
to
provide code that executes when the client enters or leaves the stopped
state.
Returns:
- Type:
-
void
subscribe(channelOrSubId, mode, bodyOptsopt) → {Subscription}
Creates a subscription to the specified channel.
When you create a subscription, you can specify additional options in the
bodyOpts
parameter. For example, you can specify a streamview or specify what the
SDK does when it resubscribes after a reconnection.
The callback function you specify for the subscription always receives PDUs in the form of JavaScript objects.
- Source:
- See:
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
channelOrSubId |
string
|
Contains a channel name or a subscription id. If you don't
specify the |
|||||||||||||||||||||||||||||||||||||||||||||||||
mode |
RTM.SubscriptionMode
|
Contains flags that determine the resubscribe behavior of
the RTM SDK and RTM. See |
|||||||||||||||||||||||||||||||||||||||||||||||||
bodyOpts |
object
|
<optional> |
{} |
Contains additional options for the subscription
|
Throws:
-
thrown if mandatory parameters are missing or invalid.
- Type
-
TypeError
Examples
// Creates a new RTM client
var rtm = new RTM('YOUR_ENDPOINT', 'YOUR_APPKEY');
// Creates a subscription with the name 'your-channel'
var subscription = rtm.subscribe('your-channel', RTM.SubscriptionMode.SIMPLE);
// Writes incoming messages to the log
subscription.on('rtm/subscription/data', function (pdu) {
pdu.body.messages.forEach(console.log);
});
// Starts the client
rtm.start();
// Creates a new RTM client
var rtm = new RTM('YOUR_ENDPOINT', 'YOUR_APPKEY');
// Subscribes to the channel named 'my-channel' using a streamview
var subscription = rtm.subscribe('my-filter', RTM.SubscriptionMode.SIMPLE, {
filter: 'SELECT * FROM my-channel WHERE object.param >= 1 OR object.id == 0',
});
// Writes incoming messages to the log
subscription.on('rtm/subscription/data', function (pdu) {
pdu.body.messages.forEach(console.log);
});
// Sets a client event listener, for unsolicited subscription PDUs, that reacts to an error PDU
// by restarting the client connection. The PDU is passed as a parameter.
rtm.on('data', function (pdu) {
if (pdu.action.endsWith('/error')) {
rtm.restart();
}
rtm.start();
unsubscribe(subscriptionId, onAckopt) → {void}
Removes the specified subscription.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
subscriptionId |
string
|
Subscription id or channel name. |
|
onAck |
function
|
<optional> |
Callback function that's invoked when RTM responds to the unsubscribe request. RTM passes the
response PDU to this function. If you don't specify |
Throws:
-
thrown if required parameters are missing or invalid
- Type
-
TypeError
Returns:
- Type:
-
void
write(channel, value, onAckopt) → {void}
Writes a value to the specified channel. The client must be connected.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
channel |
string
|
name of the channel to write to |
|
value |
JSON
|
Uint8Array
|
|
|
onAck |
function
|
<optional> |
Callback function that's invoked when RTM responds to the publish request. RTM passes the
response PDU to this function. If you don't specify |
Throws:
-
thrown if required parameters are missing or invalid
- Type
-
TypeError
Returns:
- Type:
-
void
Example
// Writes the string 'value' to the channel named 'channel' and prints the response PDU.
rtm.write('channel', 'value', function (pdu) {
console.log(pdu);
})