Skip to content

Real-time events (WebSocket)

The REST API tells you how things are when you ask. The WebSocket tells you when something changes: a room becomes occupied, a temperature reading comes in, an Occu-Light is added or a device goes offline. Use it for live dashboards, digital signage or anything that should react within a second or two, without polling.

You open one WebSocket connection per topic (the kind of event you want) and Occulights pushes every new event of that kind in your organisation to you as it happens.

wss://ws.occulights.com/ws/v1/{topic}/{companyId}/0/{tokenId}
Part What to put there
topic The kind of event: presence, environment, light, sensor, error or broadcast – see Topics
companyId Your organisation’s ID – the tenant value in your token (see below)
0 The channel. Always 0
tokenId Your API token’s ID – the sub value in your token (see below)
  1. Get an API token. Create one under Tokens in the Occulights platform, as described in Getting started with the API.

  2. Read your company ID and token ID from the token. The token is a JWT. Its payload holds your company ID in tenant and the token’s own ID in sub. Decode it once and store both next to the token:

    Terminal window
    echo "$OCCULIGHTS_TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null; echo
    # {"tenant":"8089bb74-…","sub":"3f2c9a1e-…", …}

    The token ID is also shown as id when you list your tokens with GET /tokens/.

  3. Open the connection with the token in the Authorization header, exactly as for the REST API:

    Authorization: Bearer <your token>
// npm install ws
import WebSocket from 'ws';
const token = process.env.OCCULIGHTS_TOKEN;
const companyId = process.env.OCCULIGHTS_COMPANY_ID; // "tenant" in the token
const tokenId = process.env.OCCULIGHTS_TOKEN_ID; // "sub" in the token
function connect() {
const ws = new WebSocket(
`wss://ws.occulights.com/ws/v1/presence/${companyId}/0/${tokenId}`,
{ headers: { Authorization: `Bearer ${token}` } },
);
// Keep the connection open through proxies and load balancers.
const ping = setInterval(() => ws.ping(), 60_000);
ws.on('message', (data) => {
const event = JSON.parse(data.toString('utf8'));
console.log(event.sensorId, event.presenceStatus);
});
ws.on('close', (code) => {
clearInterval(ping);
console.warn('Closed', code, '– reconnecting in 5 s');
setTimeout(connect, 5_000);
});
ws.on('error', (err) => console.error(err.message));
}
connect();

If the connection is accepted you get no greeting – events simply start arriving. If the token or the IDs in the URL are wrong, the connection is refused with 401 or 403 before it opens.

Every message is a JSON object, sent as a binary WebSocket frame containing UTF-8 text. Decode it as text, then parse it as JSON. Fields with no value are left out.

Sent whenever a room’s presence state changes.

{
"sensorId": "c4b0d6f2-5a3e-4c1b-9b8e-2f1d7a6e9c10",
"presenceStatus": "occupied",
"startedAt": "2026-11-24T08:02:11.000Z",
"cursor": "0193a8e4-7c2b-7d4e-8a11-5b9f0c3d2e61"
}
Field Meaning
sensorId The Occu-Hub or standalone Occu-Light that reported it – the same ID you use with GET /presence/{id}
presenceStatus occupied, vacant, offline (the device stopped reporting), unknown or error
startedAt When this state began
durationSeconds How long the previous state lasted, when known
cursor Position in the event log – see Never miss an event

To find which room a device belongs to, look it up once with GET /sensors/{id} or GET /lights/{id} and keep the mapping.

{
"sensorId": "c4b0d6f2-5a3e-4c1b-9b8e-2f1d7a6e9c10",
"temperature": 22.4,
"humidity": 41,
"startedAt": "2026-11-24T08:05:00.000Z",
"cursor": "0193a8e9-1f0d-7a2c-b3e4-6c8d9e0f1a22"
}

temperature is in °C and humidity in % relative humidity.

sensor and light – devices added, changed or removed

Section titled “sensor and light – devices added, changed or removed”

Sent when an Occu-Hub (sensor) or Occu-Light (light) is added to your organisation, changed or removed.

{
"op": "Create",
"identifier": "6a1f3c2e-8b4d-4e9a-a7c5-0d2e1f3b4c5d",
"macAddr": "A4:CF:12:3B:9E:01",
"user": "9d2e4f6a-1b3c-4d5e-8f70-a1b2c3d4e5f6",
"cursor": "0193a8f0-2b3c-7e4d-9f10-1a2b3c4d5e6f"
}

op is Create, Update or Delete, identifier is the device ID and user is the ID of the user who made the change, if a user did. Fetch the device from the REST API to get its new details.

{
"deviceId": "c4b0d6f2-5a3e-4c1b-9b8e-2f1d7a6e9c10",
"errMsg": "sensor read failed",
"happenedAt": "2026-11-24T08:07:42.000Z",
"cursor": "0193a8f5-4d5e-7f6a-8b9c-0d1e2f3a4b5c"
}

This is the notification feed you see under the bell in the Occulights platform: devices provisioned, removed, offline or reporting errors. Unlike the other topics, it keeps a history and you can talk to it:

Send You get
{"cursor": ""} The newest notifications, as a JSON array
{"cursor": "<id>"} The notifications older than <id> – page back with the id of the last one you got. An empty array means there are no more
{"hasBeenRead": ["<id>", …]} Marks those notifications as read for this token

New notifications arrive on their own as single JSON objects:

{
"id": "0193a8f6-…",
"title": "Sensor Offline",
"message": "Sensor c4b0d6f2-… is unresponsive and has been assumed offline",
"status": "offline",
"entityId": "c4b0d6f2-5a3e-4c1b-9b8e-2f1d7a6e9c10",
"createdBy": "mqtt-presence-event"
}

A WebSocket connection can drop – your network, a deploy on our side, or a load balancer. Occulights closes connections with code 1012 when it restarts. Build your client to:

  1. Reconnect after a short wait (a few seconds, longer after repeated failures).
  2. Keep the last cursor you received on each topic.
  3. After reconnecting, catch up with the REST API: GET /events/presence/{cursor} (or /events/environment/{cursor}) returns everything that happened after that cursor. Then carry on with the live stream.

Also send a WebSocket ping every minute or so. Connections with no traffic at all are closed after about 10 minutes.

  • One organisation per connection. A connection gets the events of the company the token belongs to. Partners managing several customers need a token – and a connection – per customer.
  • One topic per connection. Open one connection for each topic you need. A handful of connections per integration is fine.
  • HTTP/1.1 only. WebSockets over HTTP/2 are not supported. Most WebSocket libraries use HTTP/1.1 by default.
  • Revoking a token stops new connections. Close your open connections when you rotate a token, so they reconnect with the new one.

Questions? E-mail support@occulights.com.