Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 427
onkami Avatar asked Jun 16 '16 08:06

onkami


People also ask

What is WebSocket topic?

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.

What is SockJS and STOMP?

The WebSocket API enables web applications to handle bidirectional communications whereas STOMP is a simple text-orientated messaging protocol. A Bidirectional WebSocket allows a web server to initiate a new message to a client, rather than wait for the client to request updates.

Does Spring boot support WebSocket?

Spring Boot includes the spring-WebSocket module, which is compatible with the Java WebSocket API standard (JSR-356). Implementing the WebSocket server-side with Spring Boot is not a very complex task and includes only a couple of steps, which we will walk through one by one.

Can I upgrade to WebSocket Spring only?

If you hit the link with your browser, you will probably get an error Can "Upgrade" only to "WebSocket" . This is because browsers not open WebSockets by default, this needs a proper client. Since we not yet implemented a real client it is hard to verify our implementation.


1 Answers

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.

like image 121
Brian Clozel Avatar answered Oct 08 '22 12:10

Brian Clozel