Bun.udpSocket() creates a bound UDP socket
To create a new bound UDP socket, call Bun.udpSocket(). The function returns a promise that resolves to a socket object. The socket is automatically assigned a port by the operating system if no port is specified. Access the assigned port via the socket.port property.
Bun.udpSocket() port option
To specify a particular port when creating a UDP socket, pass an object with a port property to Bun.udpSocket(), for example: Bun.udpSocket({ port: 41234 }). The socket will bind to that port.
UDP socket.send() method
To send a datagram from a UDP socket, call socket.send(data, port, address). The data can be a string or buffer. The address must be a valid IP address as a string. The send() method does not perform DNS resolution, as it is designed for low-latency operations. The send() method returns false if the packet could not fit into the operating system's packet buffer.
UDP socket data callback receives incoming packets
When creating a UDP socket, add a data callback in the socket option: socket: { data(socket, buf, port, addr) { ... } }. The callback receives four parameters: the socket object, a buffer containing the packet data, the source port as a number, and the source address as a string.
UDP socket connect option creates connected socket
To connect a UDP socket to a peer, pass a connect object when creating the socket: Bun.udpSocket({ connect: { port: 41234, hostname: '127.0.0.1' } }). A connected socket sets the destination address for every packet sent and restricts incoming packets to only that peer. UDP connections are implemented at the operating system level and can improve performance.
Connected UDP socket send() with no parameters
When a UDP socket is connected, the send() method can be called with just the data: socket.send('Hello'). The destination port and address are already set by the connect option.
UDP socket.sendMany() for batching packets
To send many packets at once without the overhead of a system call for each packet, use socket.sendMany(). For an unconnected socket, pass an array where every three consecutive elements describe one packet: data, target port, target address. For example: socket.sendMany(['Hello', 41234, '127.0.0.1', 'foo', 53, '1.1.1.1']). The method returns the number of packets successfully sent.
UDP socket.sendMany() on connected socket
When calling sendMany() on a connected UDP socket, pass an array where each element is the data to send to the peer. The destination is already set by the connect option. For example: socket.sendMany(['foo', 'bar', 'baz']).
UDP socket.sendMany() returns packet count
The socket.sendMany() method returns the number of packets successfully sent. If the return value is smaller than the number of packets specified, it means some packets encountered backpressure and could not be sent.
UDP socket backpressure detection
Backpressure occurs when a packet cannot fit into the operating system's packet buffer. Detect backpressure when: send() returns false, or sendMany() returns a number smaller than the number of packets you specified. No DNS resolution is performed, so only valid IP addresses can be used.
UDP socket drain callback for backpressure
When backpressure is detected, add a drain callback to the socket option: socket: { drain(socket) { ... } }. Bun calls this handler once the socket becomes writable again, allowing you to continue sending data.
UDP socket.setBroadcast() method
To enable broadcasting on a UDP socket, call socket.setBroadcast(true). This allows sending packets to a broadcast address.
UDP socket.setTTL() method
To set the IP time-to-live (TTL) for outgoing packets on a UDP socket, call socket.setTTL(value) where value is the number of network hops, for example socket.setTTL(64).
UDP socket.addMembership() for multicast groups
To join a multicast group on a UDP socket, call socket.addMembership(multicastAddress). The multicastAddress is a string containing the multicast IP address, for example '224.0.0.1'. Optionally, specify a specific interface: socket.addMembership('224.0.0.1', '192.168.1.100').
UDP socket.dropMembership() to leave multicast groups
To leave a multicast group on a UDP socket, call socket.dropMembership(multicastAddress). The multicastAddress is a string containing the multicast IP address, for example '224.0.0.1'.
UDP socket.setMulticastTTL() method
To set the TTL (time-to-live, or number of network hops) for multicast packets on a UDP socket, call socket.setMulticastTTL(value), for example socket.setMulticastTTL(2).
UDP socket.setMulticastLoopback() method
To control whether multicast packets loop back to the local socket, call socket.setMulticastLoopback(true) to enable loopback or socket.setMulticastLoopback(false) to disable it.
UDP socket.setMulticastInterface() method
To specify which network interface to use for outgoing multicast packets on a UDP socket, call socket.setMulticastInterface(interfaceAddress) where interfaceAddress is the IP address of the interface as a string, for example socket.setMulticastInterface('192.168.1.100').
UDP socket.addSourceSpecificMembership() for source-specific multicast
To join a source-specific multicast (SSM) group on a UDP socket, call socket.addSourceSpecificMembership(sourceAddress, multicastAddress) where both are IP address strings, for example socket.addSourceSpecificMembership('10.0.0.1', '232.0.0.1').
UDP socket.dropSourceSpecificMembership() to leave SSM groups
To leave a source-specific multicast (SSM) group on a UDP socket, call socket.dropSourceSpecificMembership(sourceAddress, multicastAddress) where both are IP address strings, for example socket.dropSourceSpecificMembership('10.0.0.1', '232.0.0.1').