svrooij / node-sonos-ts

:speaker: Sonos control library, use this library in your own appliction.
https://sonos-ts.svrooij.io/
MIT License
84 stars 18 forks source link

How to unsubscribe? #85

Closed hklages closed 3 years ago

hklages commented 3 years ago

Hi - me again :-) I just installed 2.2.0.

How to unsubscribe?

This code (the volume is only to demonstrate, that player is defined)

console.log('volume >>' + await player.Volume)
const x = await player.cancelAllSubscriptions()

produces:

volume >>14
(node:49824) UnhandledPromiseRejectionWarning: TypeError: player.cancelAllSubscriptions is not a function
    at cancelAll (C:\Users\hekla\Development\node-red-contrib-sonos-events\src\sonosevents-notify.js:122:28)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:49824) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
svrooij commented 3 years ago

You can call device.CancelEvents() on the device. https://github.com/svrooij/node-sonos-ts/blob/eeb96398689ae79972f7a3717e446a7453022f78/src/sonos-device.ts#L524

But the easiest way is to just remove your listener from the event emitter.

Check-out the sample here

here is the code that triggers the unsubscribe if you remove the last listener.

hklages commented 3 years ago

Hi. I did some debugging.

To subscribe: works fine: player.ZoneGroupTopologyService.Events.on(ServiceEvents.Data, data => { ... })

to unsubscribe: does not work

const x = await player.ZoneGroupTopologyService.Events.removeAllListeners(ServiceEvents.Data)
console.log('subscription canceled' + JSON.stringify(x))

Result: subscription canceled{"_events":{},"_eventsCount":2}

So the method was executed but when looking at the modified /status I get: my code modification to see all subscribtions: subscrptionCount: Object.keys(this.subscriptions),

Result: {"host":"192.168.178.61","port":6329,"subscriptionUrl":"http://192.168.178.61:6329/sonos/{sonos-uuid}/{serviceName}","listeningSince":"2020-12-19T07:30:15.782Z","subscrptionCount":["uuid:RINCON_949F3EC13B9901400_sub0000004382","uuid:RINCON_949F3EC13B9901400_sub0000004383"]}

So it seems that

svrooij commented 3 years ago

If you remove all listeners from the .Events it does unsubscribe from the service. That is why you see that if you re-subscribe a new subscription is created.

I never bothered to remove the old subscription link from the SonosEventListener (the array that is counted with the status endpoint) because the array was only used to deliver the events to the right service. And since the old subscriptions where removed from the speakers no new events would be delivered on that service.

The entries in the array are just all subscriptions, even if it automatically unsubscribed. They won’t get any events (because the the code already cancelled the subscription at the speaker). I agree that the status endpoint doesn’t respond with the correct information, but keep in mind that it is just for debugging. Since the subscriptions are actually cancelled correctly, you can check with the in the debug logging.

hklages commented 3 years ago

OK. Understand. I close this issue.

I suggest to replace the last line by subscriptionHistory: Object.keys(this.subscriptions)

or subscriptionHistoryLength.

Summary:

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 2.2.0-beta.7 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 2.3.0-beta.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 2.3.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: