I am currently trying to use WebSocket in Spring Boot Application.
The problem is that I want to send a message to a specific user, but the message is not received by the user.
First of all, I try to send a message on Java side using:
simpMessagingTemplate.convertAndSendToUser(user.getName(), "/queue/new", out);
Secondly, I am subscribed on JavaScript side as follows:
that.stompClient?.subscribe("/user/queue/new", function() {
console.log("You have received information from someone");
});
I can not see any warning or errors in logs on both: web browser and server.
However I was able to see some interesting logs on server side:
2022-09-23 09:48:01.568 DEBUG 2319 --- [boundChannel-22] o.s.m.s.b.SimpleBrokerMessageHandler : Processing SUBSCRIBE /user/queue/new id=mysubid session=cnwpb3z5
2022-09-23 09:48:01.568 TRACE 2319 --- [boundChannel-15] o.s.m.s.u.UserDestinationMessageHandler : Translated /user/queue/new -> [/queue/new-usercnwpb3z5]
2022-09-23 09:48:01.569 DEBUG 2319 --- [boundChannel-15] o.s.m.s.b.SimpleBrokerMessageHandler : Processing SUBSCRIBE destination=/queue/new-usercnwpb3z5 subscriptionId=mysubid session=cnwpb3z5 user=dominica.rosatti payload=byte[0]
2022-09-23 09:48:05.928 DEBUG 2319 --- [MessageBroker-3] o.s.w.s.s.t.h.DefaultSockJsService : Closed 1 sessions: [y1ppyujs]
2022-09-23 09:48:26.564 TRACE 2319 --- [MessageBroker-4] s.w.s.s.t.s.WebSocketServerSockJsSession : Preparing to write SockJsFrame content='h'
2022-09-23 09:48:26.564 TRACE 2319 --- [MessageBroker-4] s.w.s.s.t.s.WebSocketServerSockJsSession : Writing SockJsFrame content='h'
2022-09-23 09:48:26.564 TRACE 2319 --- [MessageBroker-4] o.s.w.s.adapter.NativeWebSocketSession : Sending TextMessage payload=[h], byteCount=1, last=true], StandardWebSocketSession[id=ba3d56ff-714e-efaf-f7bf-932a0dca2859, uri=ws://localhost:8080/stomp-endpoint/887/cnwpb3z5/websocket]
2022-09-23 09:48:26.565 TRACE 2319 --- [MessageBroker-4] s.w.s.s.t.s.WebSocketServerSockJsSession : Cancelling heartbeat in session cnwpb3z5
2022-09-23 09:48:26.566 TRACE 2319 --- [MessageBroker-4] s.w.s.s.t.s.WebSocketServerSockJsSession : Scheduled heartbeat in session cnwpb3z5
(...)
2022-09-23 09:52:17.013 TRACE 2319 --- [boundChannel-23] o.s.m.s.u.UserDestinationMessageHandler : Translated /user/dominica.rosatti/queue/new -> [/queue/new-usercnwpb3z5]
(...)
2022-09-23 09:52:17.013 DEBUG 2319 --- [boundChannel-23] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/queue/new-usercnwpb3z5 session=null payload={"id":0,"message":"Hello Wrold","recipient":null,"sender":null,"sentAt":null,"ac...(truncated)
2022-09-23 09:54:07.933 TRACE 2319 --- [MessageBroker-1] s.w.s.s.t.s.WebSocketServerSockJsSession : Preparing to write SockJsFrame content='h'
2022-09-23 09:54:07.935 TRACE 2319 --- [MessageBroker-1] s.w.s.s.t.s.WebSocketServerSockJsSession : Writing SockJsFrame content='h'
2022-09-23 09:54:07.936 TRACE 2319 --- [MessageBroker-1] o.s.w.s.adapter.NativeWebSocketSession : Sending TextMessage payload=[h], byteCount=1, last=true], StandardWebSocketSession[id=ba3d56ff-714e-efaf-f7bf-932a0dca2859, uri=ws://localhost:8080/stomp-endpoint/887/cnwpb3z5/websocket]
Which means that server side knows how to identify the user. The sessionId is correct on the client side.
The message was not received by the web browser.
I tried to debug Spring and I found something interesting which I cannot explain.
There is a class: DestinationCache which keeps information about destinations. There is a method: getSubscriptions which gets a subscription for the Map:
Map<String, LinkedMultiValueMap<String, String>> destinationCache
Unfortunately the values in the map contains empty LinkedMultiValueMap.
For example for destination: "/queue/new-usercnwpb3z5"
I'd appreciate if someone cold give some hints why the collection in the map is empty.
Thank you a lot.
