Streaming AI Responses Into Your Angular App
You know the difference between a chat box that feels alive and one that feels broken. In the broken one you type, you send, and you stare at a spinner for five seconds before the whole answer appears at once. In the alive one the first word shows up almost instantly, then the rest fills in behind it word by word. Both take the same total time. The model is generating tokens one by one either…
Streaming AI responses into your Angular app can greatly enhance the user experience by providing instant feedback and a more interactive interface. In this post, we will build a streaming chat component in Angular that demonstrates this streaming technique, without relying on any external SDKs.
There are two main ways to read a stream in the browser: EventSource and fetch. EventSource is a built-in object for Server-Sent Events, which is easy to use and automatically reconnects when the connection drops. However, it has limitations, such as only being able to send GET requests and unable to handle authentication headers or JSON bodies. Therefore, it is not suitable for most real model APIs in production.
The second option is using fetch with stream: true in the request body. This approach provides more control and allows you to set an Authorization header and send a JSON body, which is required by most LLM endpoints. By using fetch, we can retrieve a ReadableStream from response.body and pull chunks off it ourselves, decoding them as needed. This is the method we will use in this post.
The streamChatCompletion function is an async generator that sends a POST request to the API endpoint with the provided prompt and handles the streaming response. It reads the response body using a readable stream and decodes each chunk as text. It then parses the decoded text into JSON format and yields each delta chunk. The function also handles cancellation when the user leaves, and it safely types the response so the component can be used with any OpenAI-compatible endpoint.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.