Skip to main content
Native UDP datagram sockets, exposed with the literal POSIX call names: sock.bind, sock.connect, sock.sendto, sock.recvfrom, sock.send, sock.recv, sock.getsockname, sock.getpeername, sock.close. If you’ve written a UDP socket in C, the shape is already familiar — only the promise-based sendto / recvfrom are new. Payloads are binary only (Uint8Array / ArrayBuffer) and addresses are IP literals only — there is no DNS lookup on this path.

Permissions

UDP needs --allow-net on the addresses it uses:
  • Addresses are IP literals, so there is no DNS or hostname matching. --allow-net=localhost does not cover 127.0.0.1.
  • A scope with no port matches every port. That is the usual choice for ephemeral binds: --allow-net=127.0.0.1.
  • Port 0 is checked literally for an ephemeral bind. A scope like --allow-net=127.0.0.1:9000 will deny bind({ address: "127.0.0.1" }), because the check runs with port 0 before the kernel picks one.
  • IPv6 addresses with a port need brackets in the flag: --allow-net=[::1]:9000. Without a port, --allow-net=::1 is fine.
  • Denial is synchronous and uses the standard shape:
connect and sendto throw the same way, before any promise is created or any datagram is sent. send on a connected socket does not re-check the destination; the connect grant is reused.
Incoming traffic is not filtered. UDP is connectionless: once a socket is bound, recv / recvfrom accept datagrams from any source, even sources outside every --allow-net scope. Binding to 0.0.0.0 exposes the socket on all interfaces. Filter from.address in JavaScript when source identity matters.
Full model: Permissions.

Quick look

Modules vs scripts

ff forces ES module evaluation when a file ends in .mjs. For .js files, ff tries classic-script compilation first and automatically falls back to ES module evaluation when the file contains import, export, or top-level await anywhere in the source. The examples on this page wrap async code in (async () => { ... })() so they also work when pasted into classic-script contexts.

udp.socket(family?)

Creates a UDP socket — the equivalent of socket(2). Creating a socket is not permission-gated; bind, connect, and sendto are.

Methods

bind({ address?, port? })

Read the chosen port back with sock.getsockname().port. Invalid input throws (bind: invalid address, bind: invalid port, or bind: failed (address in use?)). The permission check uses the requested address and port. For an ephemeral bind that means port 0; use a portless scope unless you pin the port.

connect({ address, port })

Both fields are required. connect(2) on a UDP socket doesn’t do any network handshake — it just records the default peer so you can use send / recv:
connect is permission-checked against the peer:

await sock.sendto(data, { address, port })

Sends one datagram and resolves with the number of bytes sent:
sendto is checked against the destination on every call:
A denial throws synchronously, before the promise is returned.

await sock.recvfrom(buf)

Receives up to buf.length bytes and resolves with [data, sender]:
Only one receive may be pending per socket; a second recvfrom before the first settles throws recvfrom: RecvPending. recvfrom does not check the sender against --allow-net — see the permissions caveat above.

await sock.send(data) / await sock.recv(buf)

Connected-socket forms that skip the address argument. Both throw send: socket is not connected / recv: socket is not connected when the socket hasn’t been connected. recv resolves the data view directly (no sender object):
send reuses the grant made at connect time; there is no per-packet check.

getsockname() / getpeername()

Both return an address object. getpeername() throws getpeername: socket is not connected until connect is called:

close()

Returns undefined immediately; safe to call twice. Pending sendto / recvfrom promises reject with ...: socket closed, further method calls throw ...: socket is closed, and the slot is recycled once the kernel side drains. Closing is also what the GC finalizer does if you leak a socket.

Data and buffers

recvfrom is zero-copy: the resolved view aliases your buffer.
  • Uint8Array input → buf.subarray(0, n) (same backing ArrayBuffer, same byteOffset).
  • ArrayBuffer input → new Uint8Array(buf, 0, n).
If the datagram is larger than your buffer, it is truncated to buf.length and the rest is discarded (standard UDP behavior — size your buffer to 65507 if that matters).

Addresses

Address objects always have this shape:
  • address — IP literal. IPv6 uses the RFC 5952 compressed form (::1, 2001:db8::1).
  • family — "inet" or "inet6".
  • No DNS: "example.com" is rejected with ...: invalid address.

Errors

Permission errors are Error objects with name: "PermissionDenied" and code: "PERMISSION_DENIED"; the rest are TypeErrors with plain messages.

Full example

One portless scope covers the ephemeral server port, the dynamic sendto destination, and the connected client’s peer.

Limits

See also