socketbus-node

1.0.1 • Public • Published

SocketBus Node Library

Getting Started

  1. Create an account -
  2. Minimum requirements -
  3. Install the library -
    npm install socketbus-node
  4. Using the library -

Create a SocketBus instance

import SocketBus from 'socketbus-node'

const config = { 
    app_id: 's-1-J2PCu8g8sAejZeXx',
    secret: 'cdKBpcruwYQ96kvIaYiorbTFxRDCbVfj'
}

const socketBus = new SocketBus(config);

End-to-End Encryption

To ensure that your data is secure, you can enable End-to-end encryption under Application > Settings. This setting in combination with the option custom_encryption_key encrypts the payload and decrypts in the client when an authenticated user receives a encrypted payload.

const config = { 
    app_id: 's-1-J2PCu8g8sAejZeXx',
    secret: 'cdKBpcruwYQ96kvIaYiorbTFxRDCbVfj',
    custom_encryption_key: 'my-unique-key'
}

Authentication

const socketId = req.body.socket_id;
const channelName = req.body.channel_name;

if (/** verifies if user can access the request channel */) {
    // returns the token to the client
    return socketbus.auth(socketId, channelName, true);
}

Presence Authentication

const socketId = req.body.socket_id;
const channelName = req.body.channel_name;
const userId = /** gets the current user id */;

if (/** verifies if user can access the request channel */) {
    // returns the auth data
    return socketbus.authPresence(socketId, channelName, userId, {
        userId: /** gets the current user id */,
        name: /** gets the current user name */
    });
}

Broadcasting

const payload = {
    food: 'cupcake'
}

/** Optional */
const broadcastOptions = {
    // Id or Ids of users that should not receive the event
    ignoreUsers: ['id1', 'id2']
}

socketBus.broadcast('food-observer','new-food', payload, broadcastOptions)
    .then(response => /** Event Sent */);

Resources

API Docs - Check-out the full documentation

Related Projects

SocketBus JavaScript Client Library - JavaScript Client Library

Package Sidebar

Install

npm i socketbus-node

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

400 kB

Total Files

17

Last publish

Collaborators

  • valterlorran