onkami
onkami

Reputation: 9411

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

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.

Upvotes: 21

Views: 22056

Answers (3)

denizg
denizg

Reputation: 952

There is a bigger and more important difference not mentioned in the answers above.

Topic is auto-delete whereas queue is durable. It means that when the websocket connection is closed, the topic and its data is removed. in queue, the server can still send messages and when client connect via websocket, it receives old sent messages by server.

By the way, there is no difference in in-memory broker. This happens when using dedicated broker.

Upvotes: 7

cleitonpqz
cleitonpqz

Reputation: 138

I think the best answer for that topic would be the following from Spring Docs

The meaning of a destination is intentionally left opaque in the STOMP spec. It can be any string, and it’s entirely up to STOMP servers to define the semantics and the syntax of the destinations that they support. It is very common, however, for destinations to be path-like strings where "/topic/.." implies publish-subscribe (one-to-many) and "/queue/" implies point-to-point (one-to-one) message exchanges.

Upvotes: 9

Brian Clozel
Brian Clozel

Reputation: 59076

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.

Upvotes: 20

Related Questions