Skip to content
AIGENCY
← Back to blog

Streaming without a queue

iFairy Admin · · 1 min read

Streaming AI output does not require websockets, a broadcast server or a queue worker. Server-Sent Events over a plain HTTP response is enough.

The chain is:

  1. The driver opens a streaming HTTP request to the provider and reads the body in chunks.
  2. It yields a StreamEvent for every data: line it parses.
  3. ChatService consumes that generator, accumulates the text, and re-yields.
  4. The controller echoes each event as SSE and flushes.
  5. The browser reads the response body with fetch and a ReadableStream reader.

Because every layer is a generator, nothing buffers the whole response in memory, and the assistant message row is updated once at the end with the final text and usage.

The gotchas

  • Output buffering. ob_flush() then flush() after every event, and set X-Accel-Buffering: no so nginx does not hold the response.
  • Compression. gzip on a streamed response defeats the point. Disable it for this route.
  • Timeouts. Raise request_terminate_timeout in PHP-FPM; the default will cut long generations off mid-sentence.