Stream Data
Use the TypeScript client to stream data from a Synnax cluster.
Streaming data is useful for real-time processing, visualization, and monitoring. This page guides you through using the TypeScript client to stream data from a Synnax cluster. If you’d like a conceptual overview of how streams work in Synnax, check out the streams page.
Open a Streamer
To start streaming data, call the openStreamer
method on the client and
provide a list of channels to stream:
const streamer = await client.openStreamer(["temperature1", "temperature2"]);
Reading Frames
Then, to read the next incoming data frame, call the read
method on the streamer:
const frame = await streamer.read();
This call will block until a new frame is available. This frame may not contain data for every channel specified. For example, if we’re reading from two sensors that are being sampled by different devices at different rates, we may receive a frame containing data only for the first channel, followed by a frame containing data only for the second channel.
For more details on how to work with the returned frames, see the Series and Frames page.
Downsampling Frames
If you are interested in streaming data at a lower rate than is being written to the server, you can use the optional downsample
parameter to specify the number of frames to skip before returning the next frame:
// setting the downsample factor to n means every nth sample will be retained
// e.g. for a downsample factor of 2, if [0.13, 0.12, 0.14, 0.13] is written,
// a streamer.read() will return [0.13, 0.14]
const streamer = await client.openStreamer(["temperature1", "temperature2"],2);
Handling Partial Frames
When reading frames from a streamer, it’s important to note that a frame may not
contain data for every channel specified when opening the streamer. For example,
if we’re reading from two sensors, temperature
and pressure
, that are being
sampled by different devices at different rates, we may receive a frame
containing data only for the first channel, followed by a frame containing only
data for the second channel.
const frame = await streamer.read();
console.log(frame.at(-1))
// Output: { temperature: 25.0 }
const frame2 = await streamer.read();
console.log(frame2.at(-1))
// Output: { pressure: 1000.0 }
const frame3 = await streamer.read();
console.log(frame3.at(-1))
// Output: { temperature: 25.0, pressure: 1000.0 }
To check if a frame contains data for a specific channel, you can use the has
method:
const frame = await streamer.read();
if (frame.has("temperature")) console.log(frame.get("temperature"));
Using an Async For Loop
The streamer implements an async iterator, which can be useful for continuously processing incoming frames of data:
for await (const frame of streamer) {
// Process the frame
const latestSamples = frame.at(-1);
}
Updating the Channel List
If you need to update the list of channels being streamed, you can call the
update
method on the streamer:
await streamer.update(["temperature1", "temperature2", "pressure"]);
This method will replace the current list of channels with the new list, not add to it.
Closing the Streamer
After you’re done streaming, it’s essential that you call the close
method on
the streamer to release the network connection and other related resources:
streamer.close();
We recommend using the streamer
object within a try-finally
block when possible. This
ensures that the streamer is always closed, even if an exception is thrown:
const streamer = await client.openStreamer(["temperature1", "temperature2"]);
try {
for await (const frame of streamer) {
// Process the frame
}
} finally {
streamer.close();
}