title: udp API description: udp.socket reference — POSIX-style datagram sockets: bind, sendto, recvfrom, and connected send/recv. order: 6
udp API
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.
Quick look
Modules vs scripts
ff treats a file as an ES module only when its name ends in .mjs or
its first code is import/export. A plain .js file runs as a classic
script, where top-level await is a syntax error — so the examples on
this page wrap async code in (async () => { ... })(). Save any of them
with a .mjs extension if you'd rather use top-level await as written.
udp.socket(family?)
Creates a UDP socket — the equivalent of socket(2).
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?)).
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:
await sock.sendto(data, { address, port })
Sends one datagram and resolves with the number of bytes sent:
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.
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):
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.
Uint8Arrayinput →buf.subarray(0, n)(same backingArrayBuffer, samebyteOffset).ArrayBufferinput →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
Full example
Limits
See also
- WebSocket — message-framed transport over TCP
- http — TCP servers
- API overview — every injected global
- Limitations — every hard cap
