Stream and Send
The Stream block is used to read from data streams (e.g., a message consumed from a Kafka topic, or a partial message completion streamed from OpenAI). With the Stream block, you can:
- Read messages from a streaming integration using the Trigger section
- Apply custom business logic to each message using the Process section
- Send messages to the
onMessagefrontend event handler in Applications
The Trigger section of the Stream block is used to connect to your streaming integration.
To use this section:
- Add a block for your streaming integration
- Select the relevant Action to read messages from your streaming integration
The Stream block will execute each time the Trigger section receives a message from the streaming integration.
- Each message is sent through to any subsequent Process blocks, and can be accessed with
- If there are no Process blocks configured, each message will be automatically sent to the API's
onMessageevent handler (unless you have disabled Automatically send messages to client).
The Process section of the Stream block is used to process each message received in the Trigger block. Processing messages is optional.
To process messages received from your streaming integration:
- Enable the Process messages setting on the Stream block
- Add any number of logical blocks to your process section
- You can access the current message using
message.value(or whatever you have renamed the message to)
- Be sure to return a value from the final block in your process section - this value will be sent to the
onMessageevent handler automatically (unless you have disabled Automatically send messages to client)
The Send block is used to send messages to the frontend
onMessage event handler for APIs with response type Stream.
The Send block can be used:
- Outside of a Stream block: This pattern is useful to stream partial data to the client. For example, you could send each page of an API response as it's received rather than waiting for it to complete, or send progress messages as each step of an API completes.
- Inside of a Stream block: This pattern is less common, since by default, the Stream block automatically sends a message for each message received by the trigger. You can disable this auto-send behavior and use the Send block in order to retain control of when messages are sent (e.g., you could use the Send block to batch messages to the client.
Send outside Stream block
The example below shows how to use the Send block in between API steps to send messages indicating the progress of an API.
Send inside Stream block
As explained in the Stream block section, the Stream block automatically sends messages to the client for every message it receives (default). However, there may be cases when you want to emit messages in batches. In this case, disable the Stream block's Automatically send messages to client setting and implement the sending logic with the Send block.
The example below illustrates how you you could use the Send block inside the Process section of a Stream block to send messages in batches of 3 to the client.