20

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).

npskirk
  • 1,188
  • 1
  • 8
  • 21

1 Answers1

3

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?

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • You can create a new issue against the "Spring Framework" (aka SPR) project, with the "Messaging" component. Thanks! – Brian Clozel Jul 23 '14 at 21:50
  • 6
    As of 4.1 we also publish subscribe/unsubscribe events http://docs.spring.io/spring-framework/docs/4.1.0.RC1/spring-framework-reference/html/websocket.html#websocket-stomp-appplication-context-events. That's probably the best way to do it. – Rossen Stoyanchev Jul 30 '14 at 13:20
  • @RossenStoyanchev Yes, that looks like the ticket. In the meantime, Grails is on Spring 4.0 in its 2.4.x versions. Any ideas when using Spring 4.0? – npskirk Aug 07 '14 at 15:03
  • 1
    A ChannelInterceptor on the "clientInboundChannel" and basically keeping track of all subscribe/unsubscribe STOMP messages. – Rossen Stoyanchev Aug 19 '14 at 16:44
  • 2
    One question, I am going to use SimpleBrokerMessageHandler in prod code are there threads? You said `SimpleMessageBroker is not for production use.`. Could you elaborate why? – RMachnik Mar 14 '16 at 09:08
  • 9
    It's production ready if you only have a single instance of your application. If you have several app instances, then messages can't be shared between instances; that job is done with a "real" message broker like rabbitmq. – Brian Clozel Mar 14 '16 at 10:23
  • Why SimpleMessageBroker is not for production use? – kodmanyagha Oct 27 '17 at 23:13