Connect to WebSocket

Setup a websocket server

You can find plenty of tutorials and instructions on the Internet on how to set up a WebSocket server. Here is one example which you can run as a nodejs service.

const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');

const server = new https.createServer({
    cert: fs.readFileSync('/etc/letsencrypt/live/yourdomainhere/cert.pem'),
    key: fs.readFileSync('/etc/letsencrypt/live/yourdomainhere/privkey.pem')
});

const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws,req) {
	ws.on('message', function incoming(message) {
            wss.clients.forEach(function each(client) {
                if (client !== ws && client.readyState === WebSocket.OPEN) {
                    client.send(message);
                }
            });
	});
	if(req.url=='/thingsee-addsomehashheretocreatesomewhatprivateendpointforyourdataormakeproperwsauthentication'){
	    ws.send('Welcome to Thingsee websocket stream');
	} else {
	    ws.close();
	}
});
server.listen(3001);

Let us know the url and authentication mechanism

Thingsee Cloud can connect to WebSockets. Let us know the end-point (with hash if used) or the other authentication mechanism if you have any.

Handle the IoT data

You will receive Thingsee IoT data to your WebSocket as shown below. You can either handle that data within your WebSocket server or preferably create a client application that can process the data.

[
{
    "tsmId": 12101,
    "tsmEv": 10,
    "hall": 0,
    "hallCount": 0,
    "tsmTs": 1520416221,
    "tsmTuid": "XXXX03X2Z80562557",     // unique sensor device id
    "tsmGw": "XXXX00EFS80560445",      // unique gateway device id
}
,
{
    "tsmId": 1110,
    "tsmEv": 10,
    "batl": 100,
    "tsmTs": 1520416221,
    "tsmTuid": "XXXX03X2Z80562557",
    "tsmGw": "XXXX00EFS80560445",
}
,
{
    "tsmId": 12100,
    "tsmEv": 10,
    "airp": 101364.599,
    "lght": 6,
    "temp": 21.3,
    "humd": 21.7,
    "tsmTs": 1520416221,
    "tsmTuid": "XXXX03X2Z80562557",
    "tsmGw": "XXXX00EFS80560445",
}
,
{
    "tsmId": 1111,
    "tsmEv": 10,
    "accx": -1024,
    "accy": 64,
    "accz": -192,
    "tsmTs": 1520416221,
    "tsmTuid": "XXXX03X2Z80562557",
    "tsmGw": "XXXX00EFS80560445",
}
]
Previous
Next