Fluxio
← Back to the blog

Vue and Svelte Chat UIs With Live Token Streams

What tokens costEN

§1 Token Streaming Makes Chat Replies Feel Live

A streaming chat interface shows an AI reply while it is still being generated. Instead of waiting for a complete paragraph, the reader sees text appear progressively. This gives immediate feedback and makes longer answers feel less like a frozen loading screen.

A token is a small piece of text, sometimes a word and sometimes only part of one. However, a network update does not necessarily contain exactly one token. Your interface should append the text it receives without assuming that each update ends at a word boundary.

Vue and Svelte can both handle this pattern well. The essential design is the same: keep a list of messages, create an empty assistant message, and update that message as text arrives.

§2 A Backend Keeps Streaming API Keys Private

The browser should send the conversation to your own backend, not directly to an AI provider using a secret key. Anything shipped to the browser can be inspected. Store credentials on the server and let the backend make the authenticated AI request.

Your backend then forwards the generated text to the browser as a stream. This also gives you a place to validate requests, check user access, and limit spending. Make sure your hosting platform supports streaming responses without buffering the entire answer first.

Fluxio fits this backend role when you want a streaming-first, pay-per-token AI API relay with one key for multiple providers. Its API base is https://api.fluxioapi.com, and OpenAI-style clients add /v1. You can use an OpenAI SDK on the server and enable streaming, then forward the resulting text to your frontend.

§3 SSE and Fetch Carry the Reply to the Browser

Server-Sent Events, or SSE, is a text-based format commonly used for AI streaming responses. It lets a server send successive events over one open HTTP response. Unlike a WebSocket connection, it is designed for server-to-browser updates rather than two-way messaging.

For a chat request that sends conversation history in a POST body, browser fetch is often convenient. Your application submits the messages and reads the response body incrementally. The built-in EventSource API is less convenient here because it does not directly support POST bodies or custom authorization headers.

Network chunks are not complete events. Keep a buffer, decode incoming bytes safely, and extract complete events before parsing their contents. A single event can span multiple reads. Alternatively, let your backend parse the upstream events and expose a simpler streaming format that your frontend understands.

§4 Vue Reactivity Updates the Assistant Message

In Vue, store the conversation in reactive state, such as a ref containing an array of message objects. When the user submits a prompt, add the user message and an empty assistant message. Give each message a stable identifier for rendering.

As text arrives, append it to the assistant message's content. Vue tracks these changes and refreshes the displayed text. Keep separate state for loading and errors so the interface can distinguish an active reply from a failed request.

§5 Svelte State Follows the Same Streaming Pattern

In Svelte 5, a message array created with $state can drive the conversation display. Add the placeholder assistant message before reading the stream, then update its content as new text becomes available.

Use a keyed each block so every message keeps its identity during updates. For either framework, frequent tiny updates can become expensive with complex formatting. If rendering feels choppy, collect incoming text briefly and update the visible message in small batches.

§6 Stop Controls and Safe Rendering Finish the Experience

  • – Connect a Stop button to an AbortController, and have the backend cancel the upstream request when possible.
  • – Preserve partial replies after an error and offer a clear retry action.
  • – Auto-scroll only when the reader is already near the bottom of the conversation.
  • – Render streamed content as text initially, and sanitize any HTML produced by a Markdown renderer.
  • – Announce generation status accessibly without making screen readers announce every tiny text update.

Test slow connections, interrupted requests, and empty replies before polishing animations. A dependable stream matters more than a typing effect.

Sign up for Fluxio to try streaming chat.

Create a Fluxio account →

More from the current