@droplit/sdk
v0.3.5
Published
Give your Node and Web apps instant cloud control
Downloads
17
Readme
Droplit.io SDK
This SDK gives your application access to the droplit.io REST Api and real-time notifications through websockets!
Checkout droplit.io
About
This library is designed for both NodeJS and browser applications.
Server Notifications: We don't recommend using this SDK for server applications. If you are looking to get server notifications, consider using our Webhooks
Setup
yarn add @droplit/sdk
# or
npm install @droplit/sdk
Usage
Rest API
import * as Droplit from '@droplit/sdk';
const token = process.env.DEVELOPER_TOKEN; // Or User Account token or Server API Key
const droplit = Droplit.instance();
droplit.initialize(Droplit.HOST.IOE, token);
// User Accounts and Server API Key's are not permitted to call ecosystems.list()
droplit.ecosystems.list().then((response) => {
console.log(response.body);
}).catch(error => {
console.log(error);
})
Websockets
Initilize websocket connection
import * as Droplit from '@droplit/sdk';
const token = process.env.DEVELOPER_TOKEN; // Or User Account token
const socket = new Droplit.Websocket.Client(Droplit.Websocket.HOST.WS, token);
socket.on('connectSuccess', () => {
console.log('ws client connected.');
});
socket.on('sessionStartSuccess', (sessionId) => {
console.log('sessionId', sessionId);
// Persist sessionId to resume sessions
});
socket.on('serviceNotification', (serviceNotification) => {
console.log('serviceNotification', serviceNotification);
});
socket.on('dataNotification', (dataNotification) => {
console.log('dataNotification', dataNotification);
});
socket.on('close', (code, reason) => {
console.log('disconnected', code, reason);
// For `code` values, see Droplit.Websocket.CustomWebsocketStatusCodes and UniversalWs.StatusCode
});
socket.on('error', (error) => {
console.log(error);
});
Subscribe to changes (managed by rest API calls)
import * as Droplit from '@droplit/sdk';
const deviceId = process.env.DEVICE_ID;
const token = process.env.DEVELOPER_TOKEN; // Or User Account token
const droplit = Droplit.instance();
droplit.initialize(Droplit.HOST.IOE, token);
const socket = new Droplit.Websocket.Client(Droplit.Websocket.HOST.WS, token);
socket.on('connectSuccess', () => {
console.log('ws client connected.');
});
socket.on('sessionStartSuccess', (sessionId) => {
console.log('sessionId', sessionId);
// To subscribe to a resource, you must supply a sessionId and specify `subscription: 'add'`
droplit.device.info(deviceId, {
sessionId: sessionId,
subscription: 'add'
}).then(response => {
console.log(response.body);
})
});
socket.on('serviceNotification', (serviceNotification) => {
console.log('serviceNotification', serviceNotification);
});
socket.on('dataNotification', (dataNotification) => {
console.log('dataNotification', dataNotification);
});
socket.on('close', (code, reason) => {
console.log('disconnected', code, reason);
});
socket.on('error', (error) => {
console.log(error);
});
Notes
HTTP
HTTP requests are made using isomorphic-fetch.
Websockets
Websocket transport layer is enabled by universal-ws, which provides a symmetric node/browser websocket api.