Fluxio
← Back to the blog

Live AI Replies: WebSockets, SSE, and Token Streams

Build notesEN

§1 Token Streams Behind Live AI Chat Interfaces

Users judge AI chat products by responsiveness, and a reply that appears token by token feels interactive long before the full answer exists. Two HTTP-native transports carry those partial tokens to the browser: Server-Sent Events and WebSockets. Both move data in real time, yet each makes different promises about direction, reconnects, and connection lifetime, so the transport you choose shapes the whole product.

Server-Sent Events delivers text frames from server to client over a long-lived HTTP response, which is a natural fit for model output that only flows in one direction. WebSockets upgrade a normal HTTP request into a persistent channel that carries prompts, partial replies, cancellation signals, and presence events on both sides. This five-minute quick start covers both paths, from the smallest working stream to the failure modes that break real-time chat in production.

The examples below assume a streaming-capable model endpoint. If you would rather not host inference yourself, a streaming-first, pay-per-token relay such as Fluxio handles generation for you, with one key reaching Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, Grok and more. The relay replies token by token at https://api.fluxioapi.com, and OpenAI-style clients simply add /v1 to the base URL.

§2 Server-Sent Events Streaming One Token at a Time

SSE is the fastest path to live AI chat because the browser reconnects automatically and the transport needs no new protocol. Your endpoint returns a text/event-stream content type and then writes one data line per token, flushing after every write so proxies and runtimes do not accumulate output. A model like Claude Sonnet 5.5 can emit hundreds of tokens per second, and the client renders each event as it arrives while a final done event closes the answer.

A minimal SSE stream includes the following pieces:

  • – a response content type of text/event-stream with response caching disabled
  • – a flush call immediately after every token so buffered chunks never stall the reply
  • – an event identifier or sequence number that lets the browser resume after a dropped connection
  • – a heartbeat comment sent every fifteen seconds to keep idle connections alive
  • – a terminating event carrying the finish reason and token usage for billing

Browser support is built in through the EventSource object, which reconnects on failure and reports the last event identifier automatically. EventSource only issues GET requests, so authenticated AI chat clients often stream through fetch with a readable body instead, which allows the bearer token that relays like Fluxio require. Either way, SSE keeps the server stateless and makes horizontal scaling as simple as placing a load balancer in front of more stream workers.

§3 WebSockets Sessions for Interruptible Real-Time Chat

A WebSocket begins with an HTTP upgrade handshake and then exchanges small frames over a persistent connection. That bidirectional path matters for AI chat because the user may interrupt generation, edit a prompt mid-answer, or ask a follow-up while tokens are still arriving. Control messages and token streams share one socket, so the client can send a cancellation frame and the server stops generation within milliseconds.

A practical WebSocket chat protocol usually includes:

  • – a client message carrying the conversation history and the chosen model identifier
  • – a stream of server frames shaped as token deltas, tool call chunks, or usage summaries
  • – a cancel message that stops generation and closes the current answer cleanly
  • – ping and pong frames proving the connection is alive through corporate proxies
  • – a resume token that restores an in-flight answer after a network blip

WebSockets do carry extra responsibilities. You must track connections in memory or in a shared store, handle reconnection yourself, and watch the buffered amount on the socket so a slow consumer cannot cause unbounded memory growth. The extra complexity pays for itself quickly in chat interfaces that need interruptions, voice input, or shared cursors.

§4 Reconnects, Timeouts, and Backpressure in Live Token Streams

Streaming failures rarely look like crashes; they look like a reply that quietly freezes halfway through. Load balancers commonly close idle responses after sixty seconds, so a stream that pauses during long tool calls must emit keepalive comments or pings to survive. TLS terminators and reverse proxies also buffer responses unless you explicitly disable buffering on the streaming path.

Design for these failure modes before launch:

  • – retry logic that resumes a token stream from the last acknowledged sequence number instead of replaying the full answer
  • – client-side rendering that tolerates duplicate events because reconnections can repeat the final chunks
  • – cancellation propagation that stops the upstream model call when the user closes the tab
  • – usage accounting per streamed message so pay-per-token billing matches exactly what the user received
  • – timeouts on both sides that distinguish slow generation from a dead connection

Simplify the reconnect story by making every answer resumable and idempotent. A streaming relay such as Fluxio takes care of provider failover and token-level delivery, so your application focuses on rendering rather than on keeping nine provider connections healthy. Backpressure on the client is mostly a rendering concern, but the server should slow reads when the socket buffer fills instead of letting tokens pile up in memory.

§5 Fluxio Streaming Relay for Multi-Model AI Chat

Fluxio is a streaming-first, pay-per-token AI API relay built for exactly these patterns. One key reaches Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, Grok and more, and replies arrive token by token through a single OpenAI-style interface at https://api.fluxioapi.com, where OpenAI-style clients append /v1. Because the interface matches the OpenAI SDK, the same streaming code works with SSE in the browser and with WebSocket gateways in your backend.

To wire a five-minute prototype, follow this sequence:

  • – create one Fluxio key and point your client at https://api.fluxioapi.com for relay access
  • – set the OpenAI SDK base URL with /v1 appended and enable streaming on the chat completion call
  • – stream tokens into the UI through SSE for read-only chat or through a WebSocket when you need interruption
  • – switch the model field between Claude Sonnet 5.5 and GPT-6.1 Sol without changing the transport code

The relay also works with Claude Code, Codex, Gemini CLI, and any OpenAI SDK, so one key covers terminal tools and production chat services alike. Members who invite friends earn 17% of what those friends pay, which helps small teams offset growing token costs. Pick SSE when the answer only needs to flow outward, and add a WebSocket layer the moment users expect to talk back while tokens stream.

Sign up for Fluxio and stream your first reply today.

Create a Fluxio account →

More from the current