Skip to content

Commit 5292a50

Browse files
committed
doc: Add notes how addMembership should be managed
Fixes: #12572
1 parent e2015b5 commit 5292a50

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/api/dgram.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ Tells the kernel to join a multicast group at the given `multicastAddress` and
9595
one interface and will add membership to it. To add membership to every
9696
available interface, call `addMembership` multiple times, once per interface.
9797

98+
UDP works as a bit special with cluster. When `dgram` creates a socket in the
99+
master process, each worker gets a copy as in `dup(2)``, except the actual
100+
mechanism is `sendmsg(2)`` with a SCM_RIGHTS vector. Therefor, `.addMembership`
101+
needs to be managed to be called only once.
102+
103+
```js
104+
const cluster = require('cluster');
105+
const dgram = require('dgram');
106+
107+
if (cluster.isMaster) {
108+
cluster.fork(); // ok
109+
cluster.fork(); // EADDRINUSE
110+
} else {
111+
const s = dgram.createSocket('udp4');
112+
s.bind(1234, () => {
113+
s.addMembership('224.0.0.114');
114+
});
115+
}
116+
```
117+
98118
### socket.address()
99119
<!-- YAML
100120
added: v0.1.99

0 commit comments

Comments
 (0)