WebSocket ConvertAndSendUser failed to send message to specific user

27 views Asked by At

I try to do a live-streaming bidding. So when the user wins the bid, he is the only person that will receive a message like "You won. An order has been placed."

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic","/user","/queue");
    config.setApplicationDestinationPrefixes("/app");
    config.setUserDestinationPrefix("/user");
}

The above is my configuration of my MessageBroker.

messagingTemplate.convertAndSendToUser(String.valueOf(winnerId), "/queue/notifications",
                    new BidMessage("AUCTION_WON", "You won the auction.", productId));

I have also set a Principal that uses userId as the identifier:

public class CustomHandshakeHandler extends DefaultHandshakeHandler {
    @Autowired
    private UserClientService userClientService;
    @Override
    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
    // Extract JWT token from request
    String jwtToken = extractJwtToken(request);
    System.out.println("determining user"+jwtToken);
    String userId = userClientService.getUserIdFromToken(jwtToken);
    // userId will become the identifier to send personal message
    return () -> userId;
}

My frontend listens to: /user/queue/notifications:

stompClient.subscribe('/user/queue/notifications', (message) => {
                console.log("inside")
                const notification = JSON.parse(message.body);
                console.log("notification",notification)
                if (notification.type === "AUCTION_WON") {
                    // Handle the notification for the winner
                    alert(notification.message); // or update a state to show in the UI
                }
            });

It seems userId is used as the identifier. As I can see from Inspect->Network->WS. It shows user-name: 3. So if the winner is 3, the winner should receive the notification. But I cannot even print out "inside". Any suggestion on where I am missing? : enter image description here

1

There are 1 answers

0
Yoey On

I finally figured it out. I just removed the "/user".

config.enableSimpleBroker("/topic","/queue")

/user channel is already managed by Spring STOMP