Difference between /topic, /queue for SimpleMessageBroker in Spring Websocket + SockJS

Askar Ibragimov picture Askar Ibragimov · Jun 16, 2016 · Viewed 12.9k times · Source

Is there a clarification, what is the differences between /topic, /queue etc. for Spring Websocket + SockJS in case I am using "simple broker" ? E.g. here Sending message to specific user on Spring Websocket it is said: when your client subscribe to an channel start with /user/, eg: /user/queue/reply, your server instance will subscribe to a queue named queue/reply-user[session id]

I would like to understand the logic behind such conversions in some clear way.

Answer

Brian Clozel picture Brian Clozel · Jun 16, 2016

You should take a look at this part of the reference documentation. In a nutshell, "/topic" and "/queue" are both prefixes configured to the same destination.

In the documentation, "/app" is the configured "application destination prefix" - meaning all messages flowing in through the "clientInboundChannel" and matching those prefixes will be mapped to your application, for example with @MessageMapping annotations.

Here also, "/topic" and "/queue" are both prefixes configured as STOMP destinations - meaning all messages flowing in through the "clientInboundChannel" and matching those prefixes will be forwarded to the STOMP broker. In your case, that's the simple broker implementation.

So from Spring Websocket's point of view, "/queue" and "/topic" are treated the same way and are "typical" STOMP destinations - all messages matching those are forwarded to the messages broker. Now if you're using a full message broker implementation, those destinations may not have the same meaning and the message broker behavior could be different. Here are some examples with Apache Apollo and RabbitMQ.

Note that if you want to, you can change those prefixes. But I would advise you to keep those as defaults unless you really know what you're doing.