Streams
Motia Streams are a way to quickly push updates from your asynchronous workflows to the client without having to implement any sort of polling processes.
How it works
You first need to define a stream in your project
Defining a stream
To be able to use Motia Sockets, you need to define a stream
Create a file called open-ai.stream.ts
under steps/
folder
Once a stream is created, it should be immediately available in FlowContext (make sure to have motia running on the project)
Then you can simply create records using the streams API in your step
The previous step just prepares a message to be created by Open AI via OpenAI SDK stream, which will be populated in the next step
Testing Streams in Workbench
We know testing real time events is not easy as a backend developer, so we've added a way to test streams in the Workbench.
Here are the steps to test streams in the Workbench:
- The API Step that provides a stream item should return the object
- Navigate to http://localhost:3000/endpoints in your Workbench
- Open up your endpoint and click on the
Test
button - The result will automatically be streamed from the server to the client streaming it's state real-time.
Consuming stream on the browser
Then add the provider to the root of your project
then on your component or hook, just use