Search code examples
javaspring-bootrabbitmqspring-websocketsockjs

Spring Boot + Spring Web Socket + RabbitMQ Web STOMP


I am working on adding live notification in my application

I have done POC with - Spring Boot - Spring WebSocket - SockJS - RabbitMQ STOMP plugin

I read about RabbitMQ Web STOMP and want to do POC of that. But it says Since version 3.7 support for SockJS websocket emulation was removed.

Is there any example for Spring WebSocket + RabbitMQ Web STOMP with or without SockJS.

Please help.

Reference links:

http://www.rabbitmq.com/stomp.html

http://www.rabbitmq.com/web-stomp.html

https://spring.io/guides/gs/messaging-stomp-websocket/


Solution

  • @n.sharvarish... first create a websocket configuration class over rabbitmq like this...

    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
        private static final Logger log = LoggerFactory.getLogger(WebSocketConfig.class);
        @Value("${spring.rabbitmq.username}")
        private String userName;
        @Value("${spring.rabbitmq.password}")
        private String password;
        @Value("${spring.rabbitmq.host}")
        private String host;
        @Value("${spring.rabbitmq.port}")
        private int port;
        @Value("${endpoint}")
        private String endpoint;
        @Value("${destination.prefix}")
        private String destinationPrefix;
        @Value("${stomp.broker.relay}")
        private String stompBrokerRelay;
        @Override
        public void configureMessageBroker(final MessageBrokerRegistry config) {
            config.enableStompBrokerRelay(stompBrokerRelay).setRelayHost(host).setRelayPort(port).setSystemLogin(userName).setSystemPasscode(password);
            config.setApplicationDestinationPrefixes(destinationPrefix);
        }
        @Override
        public void registerStompEndpoints(final StompEndpointRegistry registry) {
            registry.addEndpoint(endpoint).setAllowedOrigins("*").withSockJS();
        }
    }
    

    and then ... use above config class where you want to use .. like this..

    @Autowired
    SimpMessagingTemplate template
    
    template.convertAndSend(destinationurl, object);
    

    and configure above username and password and all from application.properties