Stream and Send
Stream
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
onMessage
frontend event handler in Applications
Trigger
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
message.value
. - If there are no Process blocks configured, each message will be automatically sent to the API's
onMessage
event handler (unless you have disabled Automatically send messages to client).
Process
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
Notes
- 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
onMessage
event handler automatically (unless you have disabled Automatically send messages to client)
Learn more on how to stream these messages to the client (e.g., the frontend of an Application) in this section of the Streaming Applications overview.
Send
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.