WebSocket SDK


The Droplit WebSocket SDK provides NodeJS or a web application with real-time device state change notifications. Application UIs can use this to dynamically update when the states of devices change.

The WebSocket SDK source code an be found on GitHub.

The SDK can subscribe to devices or environments.

Do not use this SDK for servers. To get notifications from servers, use webhooks, which are described in the platform overview.

It is possible that several events will be captured by the websocket simultaneously. In the case of such an occurrence, each event will have its own entry in the "items" array of each transaction.


The WebSocket SDK can be installed using Bower, NPM. It can also be used with Browserify, if desired.


Install the SDK with the following command.

bower install droplit-websocket-sdk --save

After referencing droplit-socket-sdk.js in html, DroplitWebSocketClient is declared in the global scope, and a new instance of a Droplit socket can be created.

var droplitSocket = new DroplitWebSocketClient.DroplitClient();


Install the SDK with the following command.

npm install droplit-websocket-sdk --save

To use the SDK, reference the package droplit-websocket-sdk.

var droplitSDK = require('droplit-websocket-sdk');
var droplitSocket = new droplitSDK.DroplitClient();


Browserify must be installed globally to ensure it works with the SDK.

npm install -g browserify
browserify index.js > bin/droplit-websocket-sdk.js --standalone DroplitWebSocketClient

SDK Events

The droplitSocket instance emits events when something changes in a resource that it is monitoring. Events contain a variety of information about the resource that spawned them, as shown.

interface payload {
    type: string,
    ecosystemId: string,
    environmentId: string,
    deviceId: string,
    service: string,
    index: string,
    member: string,
    value?: any

The “type” property can be any of the following values, depending on what action caused the event.

Service Events

Service events spawn websockets when a device's service properties are modified or its service methods are called.

Types of service events include:

  • Call: A method was called on the resource.
  • Changed: Any resource property has been changed.
  • Error: A device error was thrown.
  • Event: A service property event was emitted.
  • Get: A resource property was refreshed.
  • Info: Resource information was received.
  • Set: A resource property was explicity set.

Data Events

Data events spawn websockets when a device itself is modified.

Types of data events include:

  • Created: A device has been created.
  • Updated: A device's data has been updated.
  • Deleted: A device has been deleted.


This event type is received when the subscribed resource emits the event payload data specified above.

droplitSocket.on('event', function (data) {
    console.log("event", data);


Once a socket connection is opened, it must then be authenticated. When an authenticateRequest event is receieved, the user authorization token must be supplied. Resources cannot be accessed until this happens.

Use the authenticate() method to supply the authorization token.

If the socket must be re-authenticated at a later point in time, it is safe to use 'authenticateRequest` and 'authenticate()' in this way again.

droplitSocket.on('authenticateRequest', function () {
    // Call authenticate()


Once this event is received, the SDK may subscribe to resources.

droplitSocket.on('authenticated', function () {
    // Subscribe to resources    


This event occurs when the web socket connection is established.

droplitSocket.on('connected', function () {
    console.log('Connection established.');


This event occurs when the web socket connection is closed. It can happen from explicitly closing the connection, or the connection being lost.

droplitSocket.on('closed', function () {
    console.log('Connection closed.');


This event occurs when the web socket connection is upgraded.

droplitSocket.on('socketInfo', function (info) {


This event occurs when the web socket connection encounters an error.

droplitSocket.on('socketError', function (error) {

SDK Methods

authenticate(authorizationToken: string)

This method should be called whenever the authenticateRequest event is received.


This example calls a custom method retreiveAuthToken to get the user authorization from the application local storage.

droplitSocket.on('authenticateRequest', function () {

subscribe(resourceId: string)

This method allows an application to receive notifications about a provided resource. The web socket must be properly authenticated for this method to work.


unsubscribe(resourceId: string)

This method allows an application to stop receiving notifications about a provided resource. The web socket must be properly authenticated for this method to work.



This method reopens the socket connection by creating a new DroplitClient instance. This method only works if the socket was explicitly closed at some time prior to calling it.



This method closes the socket connection. Any subscriptions associated with the closed socket will be destroyed, and the user will be notified of any subscriptions destroyed in that way.