becvert / cordova-plugin-zeroconf

Cordova ZeroConf Plugin
MIT License
81 stars 57 forks source link

How to query? #99

Open ZweiEuro opened 2 years ago

ZweiEuro commented 2 years ago

Hi. I understand and can use the library I think, but I don't understand how to do basic things. I can subscribe or watch something but I can never really be sure if the device is still available on that address.

Add service resolves service

But after this nothing changes anymore. Even if i disconnect the device providing the mDNS service it's not detected. What I need is to query "is this thing right now in this network available" but I am not sure if this plugin can do that.

If yes, how can i do this ?

ZweiEuro commented 2 years ago

I got it working but I am not really sure if its a good idea to restart the process all the time ...

        this.zeroconf.watch("xxx._tcp.", "local.").subscribe((res) => {
          this.handlemDNS(res, this);
        });
        this.zeroconf.unwatch("_intertechno_control._tcp.", "local.");
        this.zeroconf.reInit();

after it resolves i just restart it

emcniece commented 2 years ago

I think you might be having trouble with DNS caching. MDNS is useful for broad service discovery but it is not reliable for near-real-time device detection because most devices that deal with DNS will cache results for some amount of time. This behaviour is present in most operating systems, and some hardware like routers and gateways.

Back when I was working on an app for a lighting controller we would frequently have many devices coming online, rebooting, going offline. Since our router cached DNS records, and Android cached DNS records, our app would see all of the recent devices as "active" even though some were powered off. I never figured out how long those records were cached for, nor what part of the Android OS was responsible for caching DNS.

Our solution was to scan for devices, then show a "connecting..." dialog when a device was selected. The app would try to connect and would show an error if the IP address was unresponsive. You might try doing something similar!

Your solution looks like it might be force-clearing the OS MDNS record cache by restarting the listener. I'm not sure how this interfaces at the OS level and what process costs are associated. It's probably a bad idea to reInit() every 1ms, but you could give the user that control with a screen-pull-down-refresh action.

The watch() method should run continuously and it should detect records that get registered and unregistered, but just because a device goes offline does not mean it removes its MDNS record -unregister() is a specific action that the device must emit.

ZweiEuro commented 2 years ago

Okay, I've followed a similar thing that you are suggesting. interestingly enough my app crashes if i do it my way after a while, quick performance profiling suggests there is some kind of memory leak as it just keeps growing over time, clearly gaining more each second; until the program crashes.

ZweiEuro commented 2 years ago

@emcniece It also happens a lot that Something gets added but never resolved...