I have a chat application that is build using Spring Websokcets (with STOMP)
and the message broker I have used is RabbitMQ
.
First I will try to explain the flow little bit. In my application same user can be connected to the websocket server from different devices. To keep the things simple, each user will be subscriber to /topic/{userId}
. So when somebody sends a message to user 123
, he will be able to see that message in all his devices he is connected from, because I am just sending that message to the destination /topic/{userId}
.
My problem is before sending a message to a particular topic
, I want to check how many users are connected this topic
at this point(this number specifies the number of devices the user logged in from). If this number is 0
, I want to send this message to a separate queue.
I have checked this question they look very similar to my problem. But I believe that my problem is even simpler. I just want to find out the number of users, I don't need any information about them.
In the question I mentioned above Brian Clozel mentioned that it's possible to get the information about subscription from RabbitMQ REST API, I have searched about how to do this.
If I want to know the number of users that are subscribed to a topic /topic/123
, how do I find it out using RabbitMQ API?
I have also checked this this ticket, and it seems there is added support for this use-case but as far as I understood, it will work only with SimpleMessageBroker
. Can somebody clarify If I am correct.
I will be able do this using Application Context Events
but this would work only for a single server. In a distributed system this will not work. So I thought getting that information from RabbiMQ
is the right way of doing it.
UPDATE
Let me explain more clearly what I am trying to do. Say I am a user with userid 123
and If I am logged in to the site from three devices Laptop1
,Laptop2
and Mobile1
. Initially all of them will be subscribed to /topic/123
(123
is UserId here).
Basically I want to solve two problems:
1) I don't want to keep the websocket connection open even if the user is inactive, so I close the websocket connection after a certain time of inactivity. In reality even If the mobile is inactive, it should be able to get the notifications, So my app will be pinging a notification server
to get the notification(not just messages, any kind of notifications). Now say there is an incoming message for 123
, if I know that there are no consumers connected to /topic/123
and if the user 123
has our app installed(somehow I will know this information) I want to send push this message to the notification server
so that the mobile app will be able to get the message even if the websocket connection is closed.
2) second problem is, say all three connections (Laptop1
,Laptop2
,Mobile1
) are active and there are incoming messages, I want to show notification in only one of the active client (as Gmail or Facebook does). If there is an incoming message to a client and if it knows that there are more than one devices(I can send this information from server) subscribed to /topic/123
, at the client side I can decide whether to show the pop-up or not.