How to find all users subscribed to a topic in spring websockets

npskirk picture npskirk · Jul 17, 2014 · Viewed 12.3k times · Source

When making something like a chat application using Spring Websockets, it is useful to know who all is subscribed to any given topic. For, e.g. presence data displayed in the clients.

I know that I can implement ApplicationListener and keep my own list of "connected users", but it seems like the library must already be doing this work.

What's the recommended way to get active subscription info from the library directly (and without maintaining my own list in memory or db).

Answer

Brian Clozel picture Brian Clozel · Jul 23, 2014

You're right, you could use ApplicationContext events, but unfortunately those events deal with user sessions events and broker events - so you won't be notified when a user subscribes to a particular topic.

You could do that when using the SimpleBrokerMessageHandler, by getting the SubscriptionRegistry. But again, the SimpleMessageBroker is not for production use.

If you're using RabbitMQ, you can get that information from its REST API.

The thing is, this is very specific to the broker implementation, so I'm wondering if a feature like that makes sense in Spring Framework. Could you open a JIRA issue to start the discussion?