RTM client for BearyChat
npm install bearychat-rtm-clientCompatible with Node.js, Webpack and Browserify.



- Install
- Usage
* API
+ RTMClient
* Browser usage:
* Node.js usage:
* Client events
* Client state
* RTM events
* Examples
- LICENSE
``bash`
npm install bearychat-rtm-client --save
or with yarn`bash`
yarn add bearychat-rtm-client
#### RTMClient
constructor({ url, WebSocket })
| Param | Description |
| ---- | ---- |
| url | a websocket url or a function returns a promise that resolves to a websocket url |
| WebSocket | a W3C compatible WebSocket client implement |
RTMClient uses native WebSocket in browser.
`javascript
import bearychat from 'bearychat';
import RTMClient from 'bearychat-rtm-client';
const RTMClientEvents = RTMClient.RTMClientEvents;
const client = new RTMClient({
url() {
return bearychat.rtm.start({token: '
.then(resp => resp.json())
.then(data => data.ws_host);
}
});
client.on(RTMClientEvents.ONLINE, function() {
console.log('RTM online');
});
client.on(RTMClientEvents.OFFLINE, function() {
console.log('RTM offline');
});
client.on(RTMClientEvents.EVENT, function(message) {
console.log('event message received: ', message);
});
client.send({
// your message body
});
`
RTMClient need a W3C compatible WebSocket client implement. ws version 3.0.0+ is recommended.
`javascript
const bearychat = require('bearychat');
const RTMClient = require('bearychat-rtm-client');
const RTMClientEvents = RTMClient.RTMClientEvents;
const WebSocket = require('ws');
const client = new RTMClient({
url: function() {
return bearychat.rtm.start({token: '
.then(function (resp) {return resp.json()})
.then(function (data) {return data.ws_host});
},
WebSocket: WebSocket
});
client.on(RTMClientEvents.ONLINE, function() {
console.log('RTM online');
});
client.on(RTMClientEvents.OFFLINE, function() {
console.log('RTM offline');
});
client.on(RTMClientEvents.EVENT, function(message) {
console.log('event message received: ', message);
});
client.send({
// your message body
});
`
| Event | Description |
| ----- | ----------- |
| RTMClientEvents.ONLINE| client connected |
| RTMClientEvents.OFFLINE | client disconnected |
| RTMClientEvents.CLOSE | client closed |
| RTMClientEvents.EVENT | receive event message from server |
| RTMClientEvents.ERROR | error occurred |
```
INITIAL
+
error |
+-------------+ |
v + v connect
RECONNECT+------->CONNECTING<---------+CLOSED
^ + ^
| | |
| server | |
| close/ v close +
+------------+CONNECTED+---------->CLOSING
error
MIT