WebSocket
Receive Thingsee IoT data via WebSocket connection
Thingsee Cloud can connect to WebSocket servers to deliver real-time IoT data. This is useful for applications requiring push-based data delivery without polling.
Overview
%%{init: {'theme':'base','themeVariables':{'primaryColor':'#73F9C1','primaryTextColor':'#143633','primaryBorderColor':'#143633','lineColor':'#143633','secondaryColor':'#C7FDE6','tertiaryColor':'#F6FAFA','clusterBkg':'#F6FAFA','clusterBorder':'#143633'}}}%%
flowchart TB
APP[Your Application]
WS[Your WebSocket Server]
T[Thingsee Cloud]
T -->|WebSocket| WS --> APPSetup
1. Create WebSocket Server
Set up a WebSocket server that can receive connections from Thingsee Cloud.
Node.js Example:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
console.log('Thingsee connected');
ws.on('message', (data) => {
const messages = JSON.parse(data);
messages.forEach(message => {
console.log('Received:', message.tsmTuid, message.tsmId);
processMessage(message);
});
});
ws.on('close', () => {
console.log('Connection closed');
});
});
function processMessage(message) {
// Your processing logic here
}
2. Configure Authentication (Optional)
If your WebSocket requires authentication:
- Hash in URL:
wss://your-server.com/socket#auth-token - Query parameters:
wss://your-server.com/socket?token=xxx - Custom headers (if supported)
3. Request Integration
Contact Haltian support with:
- WebSocket URL (with hash/auth if used)
- Authentication mechanism details
- Any specific requirements
Message Format
Data arrives as Thingsee Message JSON arrays:
[
{
"tsmId": 12101,
"tsmEv": 10,
"hall": 0,
"hallCount": 0,
"tsmTs": 1520416221,
"tsmTuid": "XXXX03X2Z80562557",
"tsmGw": "XXXX00EFS80560445"
},
{
"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"
}
]
Client Applications
You can also create client applications that connect to your WebSocket server:
const WebSocket = require('ws');
const ws = new WebSocket('wss://your-server.com/socket');
ws.on('open', () => {
console.log('Connected to WebSocket server');
});
ws.on('message', (data) => {
const messages = JSON.parse(data);
messages.forEach(msg => {
// Handle different message types
switch (msg.tsmId) {
case 12100:
handleEnvironment(msg);
break;
case 13100:
handlePresence(msg);
break;
default:
console.log('Unknown message type:', msg.tsmId);
}
});
});
function handleEnvironment(msg) {
console.log(`Temperature: ${msg.temp}°C, Humidity: ${msg.humd}%`);
}
function handlePresence(msg) {
console.log(`Movement count: ${msg.moveCount}`);
}
Forwarding to REST API
If your system uses REST APIs, create a simple proxy:
const WebSocket = require('ws');
const axios = require('axios');
const ws = new WebSocket('wss://your-websocket-server.com');
ws.on('message', async (data) => {
const messages = JSON.parse(data);
try {
await axios.post('https://your-api.com/sensors/data', messages, {
headers: {
'Authorization': 'Bearer your-token',
'Content-Type': 'application/json'
}
});
} catch (error) {
console.error('Failed to forward data:', error.message);
}
});
Best Practices
- Handle reconnection - Implement automatic reconnection logic
- Buffer messages - Queue messages during brief disconnections
- Validate data - Verify message format before processing
- Monitor connection - Implement heartbeat/ping-pong mechanism
- Log events - Track connection states for debugging
Use Cases
| Use Case | Description |
|---|---|
| Real-time dashboards | Push updates to web clients |
| Mobile apps | Live data feeds |
| Game integration | IoT data in Unreal Engine, Unity |
| Event triggers | Immediate response to sensor events |