WebSocket Real-time Updates
SpatialFlow provides WebSocket connections for streaming real-time device locations, geofence events, and dashboard statistics without polling.
Overview
WebSockets enable your application to receive instant updates as devices move and trigger geofence events. The following channels are available:
| Channel | URL | Purpose |
|---|---|---|
| Dashboard | wss://api.spatialflow.io/ws/dashboard/events/ | Aggregated stats and live activity feed |
| Geofence Events | wss://api.spatialflow.io/ws/events/geofence/ | Real-time geofence entry/exit events |
| Workflow | wss://api.spatialflow.io/ws/workflows/{workflow_id}/ | Live workflow status updates |
| Workflow Execution | wss://api.spatialflow.io/ws/workflows/{workflow_id}/executions/{execution_id}/ | Step-by-step execution progress |
| Webhook Status | wss://api.spatialflow.io/ws/webhooks/status/ | Webhook delivery status updates |
Authentication
Preferred method: Pass your JWT token via the Sec-WebSocket-Protocol subprotocol header:
Sec-WebSocket-Protocol: access_token, YOUR_JWT_TOKEN
Fallback (deprecated): Pass the token as a query parameter:
wss://api.spatialflow.io/ws/dashboard/events/?token=YOUR_JWT_TOKEN
The query parameter method is supported for backward compatibility but will be removed in a future release. Use the subprotocol method for new integrations.
Connecting
JavaScript Example
const token = 'YOUR_JWT_TOKEN';
const ws = new WebSocket(
`wss://api.spatialflow.io/ws/dashboard/events/`,
['access_token', token]
);
ws.onopen = () => {
console.log('Connected to SpatialFlow WebSocket');
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received:', data.type, data);
};
ws.onclose = (event) => {
console.log('Disconnected:', event.code, event.reason);
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};