Streaming Events
An example of how to use the REST API to stream sensor events in real-time.
Overview
When plotting life graphs or triggering certain alarms, a continuous stream of data is often preferred over periodic polling. In this example, we will see how the REST API can be used to set up a stream with some best practices and a simple retry policy.
Preliminaries
Data Connectors If you want to forward your data in a server-to-server integration, consider using Data Connectors for a simpler and more reliable service with an added at-least-once guarantee.
Basic Auth For simplicity, we here use Basic Auth for authentication. We recommend replacing this with an OAuth2 flow for integrations more complex than local experimentation.
Service Account Credentials You must create and know the credentials of a Service Account. Any role will suffice.
REST API This example utilizes our REST API to interact with our cloud. See the REST API Reference for a full list of available endpoints.
Response Format
Our REST API :stream
endpoints support two types of response formats, text/event-stream
and application/json
. They are set using the Accept
header, and defaults to application/json
.
The different headers are used in different cases.
application/json
: Returns a JSON object for each event, separated by line-breaks. Easy to parse in any high-level language and used in the code sample below.text/event-stream
: A Server-Sent Events specific format. Used by any Server-Sent Events libraries, such asEventSource
.
Stream Best Practices
The following practices should be considered when implementing a stream. They are all used in the following example.
Implement a Retry Policy The connection to a stream can be lost at any moment due to several factors that break the connection and should be handled by implementing a retry policy. The stream will always disconnect after one hour, as that's how long the access token lasts. If you have not received a single event for the full hour, the stream will disconnect with a 408 HTTP status code.
Detect Stream Disconnects Early An optional
ping_interval
query parameter may be used to make sure the client can still receive messages from the server. If no ping messages have been received in the specified interval, the client should reconnect to the stream.Filter Events By default, all event types are included in the stream. Use query parameters to reduce the number of events that need to be processed.
Authorization Header Use the
Authorization
header when possible. Some libraries—like the built-inEventSource
class in browsers—does not allow setting headers. In this case, thetoken
query parameter can be used instead.
Example Code
The following points summarize the provided example code.
Sends a GET request to the REST API to initialize an event stream.
Keep the TCP connection open while receiving events.
If the connection is lost or it's been too long between pings, retry N times before giving up.
Environment Setup
If you wish to run the code locally, make sure you have a working runtime environment.
The following packages are required by the example code.
Add environment variables for authentication details.
Source
The following code snippet implements streaming in a few languages.
Expected Output
For each new event in the stream, a line will be printed to stdout.
Last updated