0.9.3 • Published 4 years ago

bearychat-rtm-client v0.9.3

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

BearyChat OpenAPI RTM Client

Compatible with Node.js, Webpack and Browserify.

@BearyChat Build Status npm version

Install

npm install bearychat-rtm-client --save

or with yarn

yarn add bearychat-rtm-client

Usage

API

RTMClient

constructor({ url, WebSocket })

ParamDescription
urla websocket url or a function returns a promise that resolves to a websocket url
WebSocketa W3C compatible WebSocket client implement

Browser usage:

RTMClient uses native WebSocket in browser.

import bearychat from 'bearychat';
import RTMClient from 'bearychat-rtm-client';
const RTMClientEvents = RTMClient.RTMClientEvents;

const client = new RTMClient({
  url() {
    return bearychat.rtm.start({token: '<your hubot 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
});

Node.js usage:

RTMClient need a W3C compatible WebSocket client implement. ws version 3.0.0+ is recommended.

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: '<your hubot 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
});

Client events

EventDescription
RTMClientEvents.ONLINEclient connected
RTMClientEvents.OFFLINEclient disconnected
RTMClientEvents.CLOSEclient closed
RTMClientEvents.EVENTreceive event message from server
RTMClientEvents.ERRORerror occurred

Client state

                  INITIAL
                     +
        error        |
    +-------------+  |
    v             +  v        connect
RECONNECT+------->CONNECTING<---------+CLOSED
    ^                +                    ^
    |                |                    |
    |    server      |                    |
    |    close/      v        close       +
    +------------+CONNECTED+---------->CLOSING
         error

RTM events

RTM events

Examples

BearyChat shell client

LICENSE

MIT

0.9.3

4 years ago

0.9.2

4 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0-beta1

7 years ago

0.2.0-alpha7

7 years ago

0.2.0-alpha6

7 years ago

0.2.0-alpha5

7 years ago

0.2.0-alpha4

7 years ago

0.2.0-alpha3

7 years ago

0.2.0-alpha2

7 years ago

0.2.0-alpha1

7 years ago

0.1.0-alpha7

7 years ago

0.1.0-alpha6

7 years ago

0.1.0-alpha5

7 years ago

0.1.0-alpha3

7 years ago

0.1.0-alpha2

7 years ago

0.1.0-alpha1

7 years ago