Consuming an Event Stream in Remix
Open AI Chat Completion Event Stream
One limitation of a EventSource is that it only supports a GET request.
- We can't leverage the Remix
actionwe previously used. - Instead, we create a Resource route to respond to a
GET. - We can't submit the prompt as
POSTformData. - Instead, we use a query string parameter.

The parsed event.data looks like the following, where we're interested in choices[0].delta.content.
Create a Resource Route
The clientside EventSource requires a GET.
- We use a Remix Resource Route to convert OpenAI's
POSTtoGET. - A Resource Route is really just a route file that doesn't have a default export (a component).
Use a Remix Resource Route to expose OpenAI's POST as a GET
Handle the Event Stream
First we'll setup some boiler plate code.
How do we know when it's done?
When ChatGPT is finished responding, it will return with a message of [DONE]
Pull out the additional text from event.data.choices[0].delta.content
Start with a basic form
- Previously we had used
fetcher.Formbut that won't work here because we are trying to consume an EventStream. - Add an
onSubmithandler that callshandleChatGPTStream
Add some state to track the streamed response text
First we'll setup some boiler plate code.
How do we know when it's done?
When ChatGPT is finished responding, it will return with a message of [DONE]
Pull out the additional text from event.data.choices[0].delta.content
Start with a basic form
- Previously we had used
fetcher.Formbut that won't work here because we are trying to consume an EventStream. - Add an
onSubmithandler that callshandleChatGPTStream
Add some state to track the streamed response text