With Streaming in Superblocks, you can:
- Connect streaming integrations
- Read from a streaming integration in backend APIs
- Stream data to the client with the Stream API response type and onMessage event handler
- Write to a streaming integration in backend APIs
Connect streaming integrations
To get started with Streaming, set up any of following streaming integrations:
Read from a streaming integration
To read data from a streaming integration:
- Add a Stream block in a backend API
- Within the Stream block, add and configure your streaming integration step inside the Trigger section
- Optionally, apply custom processing of that message inside the Process section
For Kafka-based integrations, the Stream block is intended only for Consume actions. Produce actions should be configured outside of a Stream block.
For more details on the Stream block, see Control Blocks.
Stream data to the client
Messages emitted from streaming APIs are sent to the client. In Applications, the client is the Application UI. In Workflows, the client is the machine making the request.
Sending messages can be accomplished in different ways depending on the context.
When reading from a streaming integration using the Stream block:
- Send messages automatically by enabling the Stream block's Automatically send messages to client setting (default).
- Alternatively, disable automatic sending and send messages manually using the Send block (e.g. to send messages in batches).
When running any backend API:
- Use the Send block independently to send any backend data to the client. This is useful for streaming incremental results from APIs to the frontend outside of the context of streaming integrations.
In the case of streaming Workflows invoked using HTTP/2, these messages will be streamed back to the client machine making the request.
Stream API response type
To stream messages from the backend to the frontend of the Application, change the API response type to Stream in the API response block.
Unlike standard synchronous APIs, streaming APIs include an additional event handler,
onMessage, that is triggered for each message sent to the frontend. The next section covers how to use this event handler to update UI components with data from the stream.
Streaming APIs do not have a response variable for referencing a final output (i.e., there is no
onMessage event handler
As messages are sent from the backend to the frontend, they trigger the
onMessage event handler. This event handler receives a
message parameter which contains the current message.
To use these messages in your App UI, you'll need to store the data in a frontend state variable, and then bind your component to this state variable. To do so:
- Create a temporary state variable
- Update the state variable in the streaming API’s
- Bind the data property of a component to the state variable
Imagine we want to build a list of messages from a streaming API to be displayed in a table.
- Here we've created a state variable called
itemsand initialized it to an empty array.
- Each time a message is consumed and processed in the streaming API, the
onMessageevent handler updates the
- The Table component is bound to the state variable's value via
When the API runs, each message consumed is streamed to the
onMessage event handler which updates the state variable, resulting in a live feed of items in the Table.
Write to a streaming integration
To send data to write-compatible streaming integrations (e.g., producing messages in a Kafka-based service), add the integration step to a backend API, set the action to Produce, and configure the messages to write.