Protected
constructorProtected
Readonly
addressesReadonly
interfacesProtected
Readonly
multicastReadonly
optionsProtected
Readonly
typeProtected
Readonly
unicastReturns an array of objects containing the address information all the underlying sockets.
For UDP sockets, each object will contain address, family, and port properties.
Close the underlying socket and stop listening for data on it. If a callback is provided, it is added as a listener for the 'close' event.
Closes the underlying socket(s) and cleans all event listeners from the instance
Adds the listener
function to the end of the listeners array for the event
named eventName
. No checks are made to see if the listener
has already
been added. Multiple calls passing the same combination of eventName
and
listener
will result in the listener
being added, and called, multiple times.
server.on('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependListener()
method can be used as an alternative to add the
event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
The callback function
Adds a one-time listener
function for the event named eventName
. The
next time eventName
is triggered, this listener is removed and then invoked.
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener()
method can be used as an alternative to add the
event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
The callback function
By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The socket. unref() method can be used to exclude the socket from the reference counting that keeps the Node.js process active. The socket ref() method adds the socket back to the reference counting and restores the default behavior.
Sends the specified message via the socket
By default, the packet will be sent out to the multicast group address from all the underlying unicast sockets.
If options.useMulticastSocket
is set, the packet will only be sent out via
the underlying multicast socket; however, the behavior for this is undefined if
the options.srcAddress
is not also set as it may not be broadcasted on all
interfaces as you might expect if the instance is also bound to 0.0.0.0
or ::
.
If options.srcAddress
is set, the packet will only be sent out via the corresponding
unicast socket.
If options.dstAddress
is set, the packet will be sent via unicast to the specified
address.
If any failures are returned upon attempting to send, they will be returned as an array of those errors.
Sets or clears the IP_MULTICAST_LOOP socket option. When set to true, our own multicast packets will also be received on the local interface.
Sets the TTL of the socket
By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The socket unref() method can be used to exclude the socket from the reference counting that keeps the Node.js process active, allowing the process to exit even if the socket is still listening
Static
createCreates a new multicast socket using the supplied options.
When creating an instance, the underlying socket(s) are created, the events are mapped through to the instance of this class, the sockets are bound, and we're off to the races.
Note: the socket type (udp4 or udp6) is automatically detected based upon
the type of multicastGroup
supplied in the constructor options.
Additionally, the interface
supplied can be one of IPv4, IPv6, interface names, or undefined.
If interface
is undefined, we will listen on all available interfaces
(similar to binding to 0.0.0.0
or ::
)
If interface
is an interface name, we will listen to all addresses assigned to that interface
Creates a new instance of a MulticastSocket