Bit6 for JavaScript

Additional resources: GitHub Repo, Code Samples

Prerequisites

Add Bit6 SDK

Bit6 JS SDK v2 is packaged using Webpack and can be included via require, import or by just adding JS file to your app.

You can use a package manager such as npm or bower to add a dependency (make sure to use next tag):

# Use npm
npm install --save bit6@next
# Or use bower
bower install 'bit6#next'

Alternatively you can get the SDK from GitHub Repo. Only bit6.min.js is required.

Then, include the SDK in your app code:

<script src="bit6.min.js"></script>

Concepts

Services

Bit6 communication capabilies are packaged as self-contained independent services:

Events

Multiple components in the Bit6 SDK act as event emitters. For example AccessToken JWT container emits expired event:

accessToken.on('expired', function(t) {
  console.log('AccessToken expired', t);
});

Data Changes

Bit6 synchronizes its data with the server. When any data object changes (for example a new message is received, a new participant joins a conversation), the relevant object emits a data change event.

// Message change in a Conversation
conversation.on('message', onMessageChange);

// o - data object
// op - operation performed: 1 - add, 0 - update, -1 - delete
function onMessageChange(o, op) {
  if (op > 0) {
    console.log('Message added', o);
  }
  else if (op < 0) {
    console.log('Message deleted', o);
  }
  else {
    console.log('Message updated', o);
  }
}

Authentication

Bit6 uses JWT tokens to authenticate connections to the platform. To start using Bit6 communication services in your client application you need a valid JWT token which is generated either by your application server or using optional Bit6 Auth Service.

AccessToken

Bit6 JS SDK provides a container class to manage JWT tokens. It handles token parsing and monitors the token expiration making it easy to implement a token refresh mechanism.

var token = '... jwt token ...';
var accessToken = new bit6.AccessToken(token);
accessToken.on('expired', function(t) {
  console.log('AccessToken expired, need to renew', t);
  // Fetch the new token from your application server or Bit6 Auth service
  // Then update it in the container by calling:
  t.update('new-token-here');
});
console.log('AccessToken', accessToken);
// Authenticated user identity and deviceId
console.log('Identity', accessToken.identity);
console.log('Device', accessToken.device);
// Decoded JWT claims
console.log('Claims', accessToken.claims);