Skip to main content

addMembership

method Socket.prototype.addMembership
Jump to headingSocket.prototype.addMembership(
multicastAddress: string,
multicastInterface?: string,
): void

Tells the kernel to join a multicast group at the given multicastAddress and multicastInterface using the IP_ADD_MEMBERSHIP socket option. If the multicastInterface argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call addMembership multiple times, once per interface.

When called on an unbound socket, this method will implicitly bind to a random port, listening on all interfaces.

When sharing a UDP socket across multiple cluster workers, thesocket.addMembership() function must be called only once or anEADDRINUSE error will occur:

import cluster from 'node:cluster';
import dgram from 'node:dgram';

if (cluster.isPrimary) {
  cluster.fork(); // Works ok.
  cluster.fork(); // Fails with EADDRINUSE.
} else {
  const s = dgram.createSocket('udp4');
  s.bind(1234, () => {
    s.addMembership('224.0.0.114');
  });
}

Parameters Jump to heading

Jump to headingmulticastAddress: string
optional
Jump to headingmulticastInterface: string

Return Type Jump to heading

void
Back to top