colinbdclark / osc.js

An Open Sound Control (OSC) library for JavaScript that works in both the browser and Node.js
GNU General Public License v2.0
774 stars 117 forks source link

Can not receive broadcast and multicast #209

Closed kjkmr closed 7 months ago

kjkmr commented 7 months ago

With this code, I can receive messages to “localhost” but not to “127.0.0.1” and “192.168.0.100” (local IP address).

const udpPort = new osc.UDPPort({
  localAddress: "localhost",
  localPort: 10000,
});
udpPort.open();

The localAddress must be exact same string to receive. If I left the localAddress undefined, this library put default value “127.0.0.1” to it. So with this setting, I can receive the message to "127.0.0.1", but I can't receive messages to "localhost" or broadcast and multicast address.

According to the official node.js document, it says "If address is not specified, the operating system will attempt to listen on all addresses.”.

https://nodejs.org/api/dgram.html#socketbindport-address-callback

So, a dirty workaround for this problem is set options.localAddress to undefined before calling open().

udpPort.options.localAddress = undefined;

With this workaround, I can correctly receive multicast and broadcast messages with the code below.

const udpPort = new osc.UDPPort({
  localPort: 10000,
  metadata: true,
  broadcast: true,
  multicastTTL: 128,
  multicastMembership: [{ address: "224.0.0.1", interface: localIP }],
 });
udpPort.options.localAddress = undefined;
udpPort.open();

But for correct fix, I think the line below of src/platforms/osc-node.js (line:50) should be deleted.

this.options.localAddress = this.options.localAddress || "127.0.0.1";

I tested on Mac OS 13.4, node version 20.11.0 I hope this helps someone who are in the same problem. Thank you for a great library.

jean-emmanuel commented 7 months ago

I think localAddress: "0.0.0.0" is the canonical way to bind to all local addresses available.

kjkmr commented 7 months ago

Thank you. That worked correctly. Then why the default value is "127.0.0.1" ? I think it should be "0.0.0.0".

colinbdclark commented 7 months ago

If it helps any, @kjkmr, my rationale for defaulting to localhost is that I think generally defaults should be conservative, and do the least impactful thing out of the box. Whether I should have followed Node.js' underlying implementation is a good question, and I'm really not sure what the right answer to this is—but I do know that changing this default, which has been in place for over a decade, would be an API-breaking change.

By the way, I just wanted to say thanks for filing such a detailed ticket, I appreciate it. And thanks @jean-emmanuel for your response!