demo · v131

UDPSocket — datagrams from JavaScript

TCPSocket got most of the attention, but the Direct Sockets API ships UDPSocket too — for protocols that need datagram semantics: DNS clients, NTP, mDNS / SSDP service discovery, IoT control planes that speak custom binary. Probe connected UDP and bound multicast modes below.

iwa only Like TCPSocket, the UDPSocket constructor is only exposed in Isolated Web Apps with the direct-sockets permission policy. From a normal origin typeof UDPSocket is "undefined" and the demo will say so.
mode

connected (peer)

A connected UDPSocket pairs to one remote — like a TCP socket but stateless. Used for client-side custom binary protocols.

mode

bound (listener)

A bound UDPSocket accepts datagrams from any peer on a port. Used for service discovery, signalling, broadcast / multicast.

multicast bind probe

mDNS / SSDP listener

Bound UDP listens on a local port, then joins a multicast group through the socket's multicast controller. This is the service-discovery path for protocols like mDNS and SSDP.

Requires an IWA manifest with direct-sockets, direct-sockets-multicast, private/local network policy, and cross-origin isolation.

the api in use

// connected datagram socket
const sock = new UDPSocket({
  remoteAddress: "192.168.1.50",
  remotePort: 53
});
const { writable, readable } = await sock.opened;

const writer = writable.getWriter();
await writer.write({ data: new Uint8Array([0x12, 0x34]) });

const reader = readable.getReader();
const { value } = await reader.read();
// value: { data: Uint8Array, remoteAddress, remotePort }
// bound multicast listener for mDNS / SSDP-style discovery
const socket = new UDPSocket({
  localAddress: "0.0.0.0",
  localPort: 5353,
  multicastAllowAddressSharing: true
});
const { readable, multicastController } = await socket.opened;
await multicastController.joinGroup("224.0.0.251");

const reader = readable.getReader();
const { value } = await reader.read();
// value: { data: Uint8Array, remoteAddress, remotePort }

contrast with TCP

The companion TCPSocket demo covers the streaming-byte case. UDPSocket exists for the protocols that need message boundaries (one packet in, one packet out) or for multicast / broadcast peers — exactly the protocols that browser APIs have historically refused to expose. Hence the IWA gate.

see also