uNetworking / uWebSockets

Simple, secure & standards compliant web server for the most demanding of applications
Apache License 2.0
17.42k stars 1.76k forks source link

Accessing the subscribed topics in the close handler #1490

Closed Vincz closed 1 year ago

Vincz commented 2 years ago

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!

ghost commented 2 years ago

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.

uNetworkingAB commented 2 years ago

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

uNetworkingAB commented 2 years ago

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 commented 2 years ago

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

uNetworkingAB commented 2 years ago

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 commented 2 years ago

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

Vincz commented 2 years ago

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 commented 1 year ago

Hey guys! Any update about this?

uNetworkingAB commented 1 year ago

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

uNetworkingAB commented 1 year ago

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.

mikelgmh commented 10 months ago

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

uNetworkingAB commented 10 months ago

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

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

mikelgmh commented 10 months ago

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