Skip to main content
Fairyfly speaks WebSocket on both sides: upgrade HTTP clients with the 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 port 80, wss:// to 443. 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: localhost and 127.0.0.1 are different scopes. *.example.com matches subdomains but not the apex; * or bare --allow-net grants 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_FILE makes a private CA trusted but does not grant network access.
An out-of-scope client URL throws synchronously from the constructor:
Full model: Permissions.

Quick look: echo server + client

Server (server.js) — the websocket block makes upgrades automatic; echo everything back:
Client (client.js) — connect, send, print:

Server: upgrading connections

Add a websocket 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:
The 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:
Send binary with sendBinary:
socket.send after the client disconnected fails — always remove sockets in close, and never store an index or id on them (none exists).

Client: connecting and messaging

For 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 for fetch (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.
Permissions caveat: proxy variables are read by the runtime directly, so they need no --allow-env. --allow-net is checked against the target URL, not the proxy host; an allowed target may connect to a proxy host that is absent from every scope. Clear the proxy variables (or set NO_PROXY) when that is not intended.

Client errors

Three shapes, depending on when the failure happens. Synchronous mistakes throw immediately — every message starts with a WebSocket: prefix:
A denied target also throws synchronously, but is a PermissionDenied error rather than a TypeError:
A failed connection at runtime (refused, unreachable) does not throw and does not fire 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:
Connect clients with --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.