The watch() method was trying to resolve the promise multiple times, which doesn't work in JavaScript. Only the first discovered service would be returned.
The plugin now properly uses Capacitor's event listener pattern. Use both addListener('discover', ...) and watch() together:
import { ZeroConf } from 'capacitor-zeroconf';
// 1. First, set up the event listener to receive discovered services
const listener = await ZeroConf.addListener('discover', (result) => {
console.log(`Service ${result.action}:`, result.service);
switch (result.action) {
case 'added':
console.log('New service found:', result.service.name);
break;
case 'resolved':
console.log('Service resolved with details:', result.service);
break;
case 'removed':
console.log('Service removed:', result.service.name);
break;
}
});
// 2. Then start watching for services
const callbackId = await ZeroConf.watch({
type: '_snapcast._tcp.',
domain: 'local.'
});
console.log('Watching started with ID:', callbackId);
// 3. Stop watching when done
await ZeroConf.unwatch({
type: '_snapcast._tcp.',
domain: 'local.'
});
// 4. Remove the event listener
listener.remove();// You can still use the callback parameter if needed
const callbackId = await ZeroConf.watch({
type: '_snapcast._tcp.',
domain: 'local.'
}, (result) => {
console.log(`Service ${result.action}:`, result.service);
});watch()tried to resolve the promise multiple times- Only first service discovery worked
- Subsequent discoveries were lost
watch()returns a CallbackID immediately- Service discoveries are emitted as 'discover' events via
notifyListeners() - All discovered services are properly received
- Both event listener and callback patterns work
- Changed from
call.resolve(status)tonotifyListeners("discover", status) - Returns callback ID immediately instead of resolving multiple times
- Changed from
call.resolve([...])tonotifyListeners("discover", data: [...]) - Returns callback ID immediately instead of resolving multiple times