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 need--allow-net:
ws://defaults to port80,wss://to443. A scope without a port matches any port.- Matching uses the literal host, lowercased. There is no DNS matching:
localhostand127.0.0.1are different scopes.*.example.commatches subdomains but not the apex;*grants every host. - The handshake does not follow redirects, so the client checks once, in the constructor.
- TLS trust (
--ca,FF_CA_FILE) is separate and does not grant access. Proxy variables need no--allow-env; the net check applies to the target, not the proxy host.
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. Its presence enables upgrades; the upgrade itself fires when a request arrives with the handshake headers (Connection: Upgrade, Upgrade: websocket, Sec-WebSocket-Key) — on any path, before your handler runs. The handler only ever sees plain HTTP, so it returns normal responses for every path:
Always check the type of
data first — text and binary arrive differently:
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://, grant the TLS port:
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 --allow-net — you need both.
Client errors
Several shapes, depending on when the failure happens. Synchronous mistakes throw immediately — the message starts with aWebSocket: prefix:
PermissionDenied error:
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.
Server starts but clients cannot connect — the listener 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, the --allow-net target, and (for wss://) the CA trust setup from the fetch guide.
See also
- Permissions — flags, scopes, denial contract
- WebSocket API — full reference and proxy notes
- HTTP Server guide — the underlying listener
- Limitations — every hard cap