1. Help for Partners
  2. WebSocket - App partners

Connection

Create a connection to the App WebSocket channel.

wss://socket.<<environment>>.doshii.co/app/socket/v3?auth=<<authtoken>>&deviceId=<<deviceId>>

Given WebSockets require a continuously open session connected, it is also worth considering how an application should handle a semi-connected or unstable environment, ie: reattempting to connect after a network drop-out, a certain number of times within a given period.

For more information, see Use of WebSockets.

Parameters

Example

Connect

// "ws" WebSocket library or similar required https://github.com/websockets/ws
var btoa = require('btoa');
var WebSocket = require('ws');

var authToken = btoa(YOUR_DOSHII_CLIENT_ID);

// Establish a connection to the Doshii WebSocket
var ws = new WebSocket('wss://socket.sandbox.doshii.co/app/socket?auth=' + authToken);

// After you've connected to the Doshii WebSocket, you'll need to send heartbeats across
// every 30 seconds so that we know you're still listening.
ws.on('open', function(event) {
  function heartbeat() {
    var timestamp = Date.now();
    const appVersion = '1.2.3';
    const payload = JSON.stringify({
        doshii: {
            ping: timestamp,
            version: appVersion
        }
    });
    ws.send(payload);
  }

  heartbeat(); // Send one immediately to complete the handshake
  setInterval(heartbeat, 30000); // Then every 30 seconds or so thereafter to keep alive
});

// Good idea to also have some error handling in there too.
ws.on('error', function(err) {
  throw new Error('Doshii WebSocket error: ', err);
});

// Now that you're connected and errors handled, how about we listen for some messages from the server?
ws.addEventListener('message', function(event) {
  if (!event) return;
  console.log(event.type);
  console.log(event.data);
}