Skip to content

Emulator WebSocket endpoint: echo and paced messages - #59

Merged
RhysSullivan merged 2 commits into
mainfrom
websocket-echo
Nov 27, 2026
Merged

RhysSullivan merged 2 commits into
mainfrom
websocket-echo

Conversation

@RhysSullivan

Copy link
Copy Markdown

Lets a program hold a WebSocket open to a hosted or local emulator for as long as it likes, exchanging messages.

  • GET /_emulate/openai/websocket (wss:// hosted, ws:// locally) accepts the upgrade and echoes each message, text or binary, as it was received.
  • ?chunks=N&interval_ms=M uses the paced-answer controls, with the same limits (0 to 10,000; 0 to 60,000 ms). The server also sends N messages of its own, {"type":"emulate.message","n":1,"of":N,"at":…}, M ms apart, then stays open. So a client can hold a busy connection or an idle one.
  • Closing: it closes when the client closes, answering with the client's close code (1000 for a normal close). A plain GET answers 426.
  • Labelled: it is an emulator control in the manifest. OpenAI's own Realtime and WebSocket-mode protocols stay listed as unsupported. It is documented with the pacing controls (README, docs page, skill).

Core: upgradeWebSocket(request, session) gives routes one interface on both hosts.

  • Workers runtime: a WebSocketPair accepted in the instance's Durable Object. Binary messages arrive there as a Blob; they are read in order before the session sees them.
  • Node: the server side of RFC 6455 in serve's upgrade handler: the handshake, masked client frames, fragmented messages, ping/pong, the closing handshake, and a 1 MiB message limit. No new dependency.
  • Response handling: the response finaliser passes a 101 through untouched, and the ledger records an upgrade without reading a body.

Tests:

  • OpenAI, against Node's own WebSocket client: echo of text, binary and a 70,000-character message; paced messages at the asked spacing; still open and echoing afterwards; clean close with 1000; 426 and 400.
  • Core: the Workers branch against a stand-in WebSocketPair: Blob reading in order, binary sent as its own buffer, and the close echo.

On a staging copy of the hosted Worker: echo of text and binary; 5 paced messages in 1.0 s; clean close with 1000. Three deploys of the staging Worker while a connection was open, one of them followed by ordinary requests to the same instance, left the connection open and echoing every message.

…ocal

GET /_emulate/openai/websocket accepts a WebSocket upgrade and echoes each
message, text or binary, as it was received. With ?chunks=N&interval_ms=M,
the paced-answer controls, it also sends N messages of its own, M ms apart,
then stays open, for a busy connection or an idle one. It closes when the
client closes, answering with the client's code. An emulator control, not
OpenAI's Realtime or WebSocket-mode protocol, which stay unsupported.

Core gains upgradeWebSocket(request, session) for routes, on both hosts: a
WebSocketPair on the Workers runtime (binary messages may arrive as a Blob,
read in order), and on Node a server side of RFC 6455 in serve's upgrade
handler (masked client frames, fragments, ping/pong, the closing handshake,
a 1 MiB message limit). The response finaliser passes a 101 through, and the
ledger records an upgrade without reading a body.
@RhysSullivan
RhysSullivan marked this pull request as ready for review November 27, 2026 08:45
@RhysSullivan
RhysSullivan merged commit 8e0633d into main Nov 27, 2026
1 check passed
@RhysSullivan RhysSullivan mentioned this pull request Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant