WebSocket API
subscribe to a live sensor data feed
The event-based, real-time WebSocket API is used to subscribe to a live sensor data feed, removing the need to pull the data periodically. For new data inserts, meaning that whenever sensor data is retrieved, a message is sent to all subscribers for that specific sensor. Device data is stored in an append-only format, therefore all measurements stored in the akenza database lead to new data inserts. An API client (hereafter Client) can either subscribe to multiple devices at once or to individual device as seen fit.
The Client calls wss://api.akenza.io/v3/data-streams
(note that the base URL for private deployments of akenza is different) in order to open a new WebSocket session on the server. The client will get the following response initially if the connection has been successfully established:
Authentication
There are multiple forms of authentication:
Using the URL-Param “access_token” with a valid JWT token for the initial connection (recommended). The OAuth2 access token is retrieved from the akenza Auth service via standard flow (authorization code or implicit)
Using the URL-Param “xApiKey” for API-Keys for the initial connection. An API-Key can be created on akenza
For deferred subscriptions, the Client needs to send the access token alongside the message in the property “accessToken” when sending a subscribe message. This will ensure that the client always sends a valid JWT to the server with which it can ensure the proper permission handling. If “accessToken” is not set, the server will fall back to the initially provided token via URL-Param. Depending on token lifetime, this can lead to unexpected permission check failures.
Subscribing to Asset Data Change Streams
The Client can send the following message to the server to get live updates about the respective device data.
The provided asset ID corresponds to the Akenza ID on akenza that is used as a virtual identifier for the sensor. The id is a unique message identifier that is returned in the response.
If the subscription was initialized correctly, the server will respond with the following acknowledge message:
Sample Received Event
The following message is sent from the server to the Client after a new sample has been received for one of the active subscriptions:
The actual data format can be configured using the uplink scripts on akenza.
Message consistency
Data points are not guaranteed to be sent in the right order (time of arrival), the ordering has to be done by the Client. If the connection between the Client and Server fails for some reason (disconnect), the missing data for this time period has to be reloaded.
Errors
If an error occurs, a message in the following format is returned:
Ping-Pong
In order to keep the connection alive, the Client should send a ping message every 30 seconds. If the Client does not support the WebSocket ping specification, the following message should be sent:
When a Client sends a ping command in order to keep the connection alive, the server responds with the following message. Pong messages can be ignored by the Client since they do not hold any significant information:
Last updated
Was this helpful?