Skip to main content
These examples run from the documentation directory using Node.js 22.18+ or 24 LTS and pinned ws@8.22.0. Execute them in a terminal or application server. Browser-native WebSocket authentication differs; do not copy a long-lived API key into a webpage.

Choose a protocol

HTTP SSE uses data: / event: lines. Each WebSocket application message is JSON without those prefixes, and [DONE] is not the completion signal. ws handles framing, UTF-8 reconstruction, and ping/pong.

Environment

API_BASE_URL includes /v1 and any deployment prefix. The shared module converts HTTPS to WSS or HTTP to WS, then appends the endpoint. Credentials, query strings, and fragments are not accepted in the base URL. Realtime puts the model in the URL query; Responses puts it in create events. Use HTTPS/WSS in production. This is server-side Bearer authentication. HTTP 101 only confirms upgrade; channel selection, session configuration, and generation may still fail. Do not follow redirects automatically. Correct the API URL before retrying. See production integration for protocol, access, and proxy checks.

Shared transport module

Both examples import examples/websocket-client.mjs. Copy it alongside the example when moving to another project, and install ws@8.22.0. It is a supporting module, not a standalone request script.

Example bounds

The total deadline includes handshake, configuration, sending, and waiting for the two-turn or one-turn workflow. It defaults to 60 seconds and accepts 100–120000 milliseconds. Incoming messages are limited to 1 MiB, with at most 256 queued messages / 4 MiB. These protect the examples’ resources; they are not gateway or provider limits. Invalid JSON, binary frames, error events, early close, and timeout terminate the examples. Logs use fixed diagnostics instead of remote error bodies or keys; consult gateway logs when necessary. The examples do not automatically retry, reconnect, cancel, or execute tools. Closing the connection does not guarantee upstream cancellation. HTTP(S) proxy environment variables do not automatically configure a ws proxy agent. Integrate a verified Agent explicitly if your environment needs one. These examples connect directly to the gateway and keep TLS verification enabled. 阅读中文