onlxltd / bonjour-service

A Bonjour/Zeroconf protocol implementation in TypeScript
https://npmjs.com/package/bonjour-service
MIT License
69 stars 21 forks source link

Not discovering services on my local network #9

Open capJavert opened 2 years ago

capJavert commented 2 years ago

I have the simple setup from the README.

Service.js:

import Bonjour from 'bonjour-service'

// some code to get my http server up and running

const instance = new Bonjour()

instance.publish({ name: 'My Web Server', type: 'http', port: 3000 })

Client.js:

import Bonjour from 'bonjour-service'

instance.find({ type: 'http' }, function (service) {
  console.log('Found an HTTP server:', service) // this never triggers
})

When I publish and search services on the same computer (i am on macOS 12.1) they are discovered. But when I run the above code on two different machines eg. One computer is running Service.js and another one is running Client.js the find callback never triggers and no services are found.

I can confirm that my HTTP server is visible if I manually input the computer address and go for example to the GET /api endpoint on another computer so they definitely see each other.

Any tips on debugging this?

capJavert commented 2 years ago

After additional testing I found out that my service will be discovered if the up event happens while browser is searching for services eg:

  1. I start the search with bonjour.find
  2. I publish my service with bonjour.publish
  3. browser finds the service eg. up callback is triggered

If the publish happens before search is initiated the service never gets discovered.

mdidon commented 2 years ago

Hi @capJavert, out of interest, if you restart both machines then start Service.js followed by Client.js does it work?

capJavert commented 2 years ago

Nope

mdidon commented 2 years ago

Interesting, we've been unable to replicate this issue across two Macs on a network. We personally use the publish then find method in another project, cross platform with out any issue so I'm wondering if this is a setup specific issue.

I'll leave this open for now and update if we find anything. Thanks

capJavert commented 2 years ago

If you need any more info from me or you have questions about the setup please do tell. I am open to dig into this.

hrueger commented 2 years ago

I just had a similar problem: My Windows PC would detect devices on the network (Blackmagic ATEMs and HyperDecks) if I disconnect those devices from the network and reconnect them or if I repower them. However, if I just start the bonjour browser with the devices already running, they would not be found.

Strangely, I could solve this problem by restarting the Windows machine... I have no idea what causes this ;-)

eclazi commented 1 year ago

@capJavert did you manage to resolve this?

capJavert commented 1 year ago

@eclazi nope, I created my own protocol over thin UDP/TCP implementation. Using it for my app for quite some time.

joeltroughton commented 4 months ago

I'm running into this issue too: Devices announcing themselves on the network whilst the bonjour browser is running are picked up, but go undetected otherwise.

I did some digging in Wireshark to see if the expected traffic was being sent, and found none when monitoring the expected network interface (Ethernet). However, when monitoring a different interface (eg, vEthernet for WSL, or "Loopback Pseudo-Interface"), I can see the MDNS queries going out.

It seems the selection of the correct interface, or the broadcasting to multiple interfaces isn't working as expected. In multicast-dns I see the same behaviour (perhaps Node-related?), but there is an ability to force the interface, which makes things work more predictably. My minimum example code for testing here:

import { Bonjour } from 'bonjour-service';
import os from 'os';

var networkInterfaces = os.networkInterfaces();
console.log(networkInterfaces);

const bonjourInstance = new Bonjour({});

console.log("Starting network discovery");
let browser = bonjourInstance.find({ type: 'minismu', protocol: 'tcp' });

browser.on('up', service => {
  console.log('Device up:', service);
});

browser.on('down', service => {
  console.log('Device down:', service);
});

browser.on('error', err => {
  console.error('Error occurred:', err);
});

browser.start();