uWebSockets icon indicating copy to clipboard operation
uWebSockets copied to clipboard

Accessing the subscribed topics in the close handler

Open Vincz opened this issue 3 years ago • 1 comments

Hi! First of all, thank for this wonderful library. My problem is the opposite of uNetworking/uWebSockets.js#647 . I need to know the topics the ws was subscribed to when it closed. Accessing ws.getTopics() in the close handler return an invalid access.

Would it be possible to give the close handler an extra parameter containing the topics the ws was subscribed to when it closed? Something like: close: (ws, code, message, topics)

As an alternative, it would be great to have access to the list of active topics with the number of subscribers from the app object.

Have a wonderful day!

Vincz avatar Feb 17 '22 06:02 Vincz

Yeah this is a problem, I like how it was before the change. Maybe there needs to be two close events: closing and closed. In case someone needs to know the topics before they are gone, they need to look in closing callback.

ghost avatar Apr 20 '22 05:04 ghost

A simple unsubscribed(topic) callback would do, right? It gets called whenever you or the library unsubscribes from one topic.

uNetworkingAB avatar Nov 06 '22 04:11 uNetworkingAB

unsubscribed(ws, topic, remainingSubscribers)

That's a good callback - remainingSubscribers would be 0 when it is time to close Redis subscription

Probably don't need one for subscribe since it's always explicit

uNetworkingAB avatar Nov 06 '22 04:11 uNetworkingAB

No wait what you need is a callback for when a topic is removed due to no websockets subscribing to it anymore?

uNetworkingAB avatar Nov 06 '22 04:11 uNetworkingAB

Both you and the referenced issue is pretty much about getting a callback whenever a topic lost all its subscribers. You just have small variations of the problem

uNetworkingAB avatar Nov 06 '22 04:11 uNetworkingAB

To make it as generic as possible, it could be called topic(name, number) and be called whenever a topic changes number of subscribers. Then you can use 0 as destroy and 1 as create and whatever else as statistics or whatnot

uNetworkingAB avatar Nov 06 '22 04:11 uNetworkingAB

Hi @uNetworkingAB! On my side, I keep a value in redis with the number of subscribers for each channel. When a user subscribe, I increment and when a user unsubscribe, I decrement. The problem is when the connection is closed, I don't know what channel they were subscribed to. So for me, it was to be able to access this subscribed channels in the close event.

Vincz avatar Nov 07 '22 15:11 Vincz

Hey guys! Any update about this?

Vincz avatar Dec 09 '22 21:12 Vincz

No news but this is definitely a high prio item (pretty low hanging fruit

uNetworkingAB avatar Dec 11 '22 21:12 uNetworkingAB

This should be implemented now.

It works like so: .subscription = [](websocket, topic name, new count, old count) {}

Any time a websocket is subscribed or unsubscribed for any reason (whether explicit by call to subscribe, or implicit by socket close) it will emit this, so you can keep track of external (Redis?) subscriptions this way.

uNetworkingAB avatar Dec 24 '22 10:12 uNetworkingAB

Can you provide an example of this please? I'm trying to search for .subscription in the documentation but I couldn't find anything.

mikelgmh avatar Dec 27 '23 18:12 mikelgmh

It's a handler just like for instance .open, .close.

https://unetworking.github.io/uWebSockets.js/generated/interfaces/WebSocketBehavior.html#subscription

uNetworkingAB avatar Dec 27 '23 20:12 uNetworkingAB

Thank you! It looks like the .subscription handler is missing in Elysia.js bun framework, so it has nothing to do with this library.

mikelgmh avatar Dec 27 '23 20:12 mikelgmh