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 need --allow-net:
  • ws:// defaults to port 80, wss:// to 443. A scope without a port matches any port.
  • Matching uses the literal host, lowercased. There is no DNS matching: localhost and 127.0.0.1 are different scopes. *.example.com matches 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.
A denied 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. 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:
The upgrade shares the listener’s net grant; no separate WebSocket flag exists.

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://, 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 a WebSocket: prefix:
A denied target also throws synchronously, but is a PermissionDenied error:
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 the 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. 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