The evrythng-pubsub.js SDK is a plugin to be used in combination with evrythng.js to allow applications to directly subscribe and publish to MQTT topics for each resource.

These plugins makes it easy to communicate with the Pub/Sub Broker instead of the usual HTTP REST API.



evrythng-pubsub.js is compatible with Node.js and browsers, choosing WS or MQTT or a combination as appropriate.


Installation is similar to the evrythng.js SDK, but the library name is evrythng-pubsub instead of evrythng. Note that installation of evrythng.js first is required to use this plugin.

Install with Dependencies

<!-- evrythng.js SDK -->
<script src="https://d10ka0m22z5ju5.cloudfront.net/js/evrythng/5.2.0/evrythng-5.2.0.js"></script>

<!-- evrythng-pubsub.js SDK -->
<script src="https://d10ka0m22z5ju5.cloudfront.net/js/evrythng-pubsub/1.0.0/evrythng-pubsub-1.0.0.js"></script>

Including the library in a Node.js application and performing initialisation is identical. However, because this is a plugin, make sure to apply it to the base SDK using use():

const evrythng = require('evrythng');
const PubSub = require('evrythng-pubsub');



Before the MQTT or WebSocket connection can be used, make sure the right options are chosen using PubSub.setup(). Clients should always connect to the mqtts://mqtt.evrythng.com domain for MQTT or wss://ws.evrythng.com domain for Web Sockets.

  apiUrl: 'mqtts://mqtt.evrythng.com:8883/mqtt',
  reconnectPeriod: 1000,
  keepAlive: 50

Function Reference

The evrythng-pubsub.js SDK simply add new methods to existing resources, accessed through the same scopes. These are:

.subscribe(callback);  // Subscribe to resource updates.

.unsubscribe();  // Unsubscribe from a resource.

.publish(value);  // Publish a new value or object to a resource.

Some common examples are shown below to help illustrate this pattern:

const thngId = 'UGM4xF47eDsaQKRwRhdHpnbg';
const key = 'temperature_celsius';
const actionType = 'scans';

const thngResource = operator.thng(thngId);

// Update a Thng
thngResource.publish({ name: 'Some New Name' });

// Subscribe to a Thng property's updates

// Update a Thng property's value

// Create a Thng action

// Subscribe to all actions

// Unsubscribe from Thng property updates