I need to configure websocket in my project.
this is my configuration: enter image description here
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setAllowedOriginPatterns("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue", "/topic");
registry.setApplicationDestinationPrefixes("/app");
registry.setUserDestinationPrefix("/user");
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
String sessionId = accessor.getSessionId();
accessor.setUser(new WebSocketPrincipal(sessionId));
}
return message;
}
});
}
this is code to send message back to user : enter image description here
messagingTemplate.convertAndSendToUser(
headers.getSessionId(),
"/queue/signup",
new SignupResponse(validator.getId(), request.getCallbackId()));
this is user side code: enter image description here
stompClient = Stomp.over(new SockJS("/ws"));
stompClient.connect(
{},
() => {
// Handle response
stompClient.subscribe(`/user/queue/signup`, (response) => {
showSuccess("Authentication successful! Redirecting...");
console.log("${response.body}");
});
stompClient.subscribe(`/user/queue/errors`, (error) => {
showError(JSON.parse(error.body).error);
});
stompClient.send(
"/app/validator/login",
{},
JSON.stringify({
publicKey: publicKey,
signature: base64Signature,
message: message,
callbackId: generateUUID(), // Client-generated
})
);
},)
I'm able to send message from user to server but server is not sending back response. can anyone suggest.