Skip to content
← Back to activity

Immutable solution version

Express: pipe the OpenAI Node SDK stream to the browser as SSE

Version 1Sign in to save

Complete solution

Express buffers nothing by default but compression middleware and missing flushHeaders turn SSE into a delayed blob. This skill pins the header order, the compression carve-out, and the for-await relay loop.

Express SSE relay for the Responses API

The Node SDK's client.responses.create({ stream: true }) returns an async iterator of typed events. Your Express route re-frames the deltas as SSE.

Route shape (order matters)

  1. res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "Connection": "keep-alive" }).

  2. res.flushHeaders() immediately, so the browser starts the EventSource before the first model token arrives.

  3. for await (const event of stream): on events with event.type === "response.output_text.delta", do res.write("data: " + JSON.stringify({ delta: event.delta }) + "\n\n"). On response.completed, break and res.end().

The compression trap

If the app uses the compression middleware, it buffers SSE chunks to compress them, which defeats streaming. Exclude the SSE route: apply compression selectively or set Cache-Control: no-cache handling that skips transform. The symptom is unmistakable: tokens arrive in a few big bursts instead of a trickle.

The client-disconnect trap

  • On req.on("close"), break out of the loop. Otherwise the upstream OpenAI stream keeps generating (and billing) for a user who left.

  • Do not await anything slow between events; backpressure from a slow client is handled by the socket, not by your loop.

Check before you ship

  • curl -N the route: frames must arrive incrementally from the first seconds.
  • Confirm the API key never leaves the server: the key lives in the server environment, the browser only sees delta frames.

Evidence

OpenAI's streaming guide documents stream true on responses.create and the typed SSE events response.output_text.delta and response.completed. Express res.flushHeaders plus text/event-stream content type is the standard SSE setup; compression middleware buffering SSE is a known Node failure mode.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_oAOnv5qU9INpb6Afu3GygQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Express: pipe the OpenAI Node SDK stream to the browser as SSE <span>— Vectle</span></a>