10

I'm trying to implement a push notification in spring using websocket and with the use of sock.js.

These are the code snippets:

    public class NotifyController {

        @MessageMapping("/notifications")
        @SendTo("/get/notifications")
        public Greeting greeting(HelloMessage message) throws Exception {
            new Greeting("Hello, " + message.getName() + "!");
        }
    }


    public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
        @Override
        public void configureMessageBroker(MessageBrokerRegistry config) {
            config.enableSimpleBroker("/get/notifications");
            config.setApplicationDestinationPrefixes("/gssocket");
        }

        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            registry.addEndpoint("/notifications").withSockJS();
        }
    }

This is the code in the front..

    function connect() {
        var notificationSocket = new SockJS('/notifications');
        stompNotificationClient = Stomp.over(notificationSocket);

        stompNotificationClient.connect({}, function(frame) {
            console.log('Connected: ' + frame);
            stompNotificationClient.subscribe('/get/notifications', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });
    }

    function sendNotification() {
        var name = "test"
        stompNotificationClient.send("/gssocket/notifications", {}, JSON.stringify({ 'name': name }));
    }

I've already managed to make it work. But the question is, how can I push the message to certain target users. For example, there are 5 online users namely: user1, user2, user3, user4 and user5. I want the notification to be published to user1 and user2 only. Can you give me any idea on how to achieve this one? I'm thinking of either doing it in the backend or in the frontend. Or there is another way to achieve this using spring.

Somebody help me please.

Thank you.

1

1 Answer 1

6

You can check User destinations to target specific users with your messages, this is done using the SimpMessagingTemplate.convertAndSendToUser() call.

Note that to enable this functionality your users must be HTTP authenticated.

Sign up to request clarification or add additional context in comments.

2 Comments

But that doesn't really help. When you use SimpMessageTemplate.convertAndSendToUser, if you look at the code, it literally just prefixes '/user/{username}' and then uses the SimpMessageTemplate.convertAndSend method. It does not actually address the security concern.
Yes, it does: that's a special queue where a unique sessionId of the user is appended to the destination and transparently removed by the broker. Try it and verify that only the originating user is able to receive the messages sent there: all the other clients are not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.