websocket option (server), or dial out with new WebSocket(url) (client). Server and client share the frame protocol but have different APIs — this guide covers both.
Permissions
Both sides are network operations, gated by--allow-net:
ws://defaults to port80,wss://to443. A scope without a port matches any port.- Matching uses the URL’s literal host, lowercased. There is no DNS
resolution and no IP matching:
localhostand127.0.0.1are different scopes.*.example.commatches subdomains but not the apex;*or bare--allow-netgrants every host. - There are no redirects in the WebSocket handshake, so the client checks once, in the constructor.
- TLS trust is separate:
--ca/FF_CA_FILEmakes a private CA trusted but does not grant network access.
Quick look: echo server + client
Server (server.js) — the websocket block makes upgrades automatic; echo everything back:
client.js) — connect, send, print:
Server: upgrading connections
Add awebsocket block to the options. Any request carrying the handshake headers (Connection: Upgrade, Upgrade: websocket, Sec-WebSocket-Key) is upgraded before your handler runs — the handler never sees that request, and no { status: 101 } branch is needed (a handler that would return 404 still gets the 101). The handler serves plain HTTP only:
Always check the type of
data first — text and binary arrive differently:
websocket option is served by the same listener as plain HTTP, so one
grant covers both: --allow-net=0.0.0.0:3000.
Server: sending and broadcast
Server sockets have exactly two methods —send (text) and sendBinary (bytes). There is no socket.id, no readyState, and no server-side close. Identify peers by object reference in a Set:
sendBinary:
Client: connecting and messaging
wss://, use the default port or name it explicitly:
Only
ws:// and wss:// URLs are accepted. For wss:// to private CAs, the same trust rules as fetch apply (--ca for scripts, FF_CA_FILE for ff start). Trust does not replace the --allow-net grant — you need both.
Proxies
The WebSocket client shares the runtime’s HTTP client, so the proxy environment variables apply exactly as they do forfetch (see
fetch Proxies: HTTP_PROXY / HTTPS_PROXY /
ALL_PROXY / NO_PROXY, read once at process start. wss:// tunnels
through CONNECT. ws:// also needs a proxy that accepts CONNECT for the
target port — a forward-only HTTP proxy will reject the connection.
Client errors
Three shapes, depending on when the failure happens. Synchronous mistakes throw immediately — every message starts with aWebSocket: prefix:
PermissionDenied error
rather than a TypeError:
onerror — you get a bare onclose. When the close
does carry an error, onerror fires first with an event whose .message
is a string (not an Error), immediately followed by onclose, whose
event carries numeric .code and string .reason:
Practical example: presence counter
Track who’s online and push the count to every client on join/leave:--allow-net=127.0.0.1:3000.
Reference: limits
Troubleshooting
new WebSocket throws PermissionDenied — the URL’s host and port are
outside every --allow-net scope. Add the target exactly as written:
ws://127.0.0.1:3000 needs --allow-net=127.0.0.1:3000; localhost will not
match 127.0.0.1. The constructor throws before any socket is opened.
Server starts but clients cannot connect — http.serve is checked as
0.0.0.0:<port>, even for loopback clients. Grant
--allow-net=0.0.0.0:3000, not 127.0.0.1:3000.
Upgrade never fires — the websocket block must be present, and the client must send the handshake headers (Connection: Upgrade, Upgrade: websocket, Sec-WebSocket-Key — any real WS client does). No handler branch is needed: the upgrade happens before the handler runs.
socket.id is undefined — by design. Use a Set of socket objects; peers.has(socket) is the identity check.
Binary arrives as Uint8Array, not string — check typeof data === "string" first, per the table above.
Client connect fails — confirm the ws:///wss:// scheme, the 512-byte path limit, and (for wss://) the CA trust setup from the fetch guide.