Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 669
n.sharvarish Avatar asked Aug 08 '18 11:08

n.sharvarish


People also ask

Does RabbitMQ use WebSockets?

RabbitMQ Web MQTT plugin is rather simple. It takes the MQTT protocol, as provided by RabbitMQ MQTT plugin and exposes it using WebSockets.

Is STOMP deprecated?

This project is no longer maintained. If you encounter bugs with it or need enhancements, you can fork it and modify it as the project is under the Apache License 2.0.

Is STOMP a WebSocket?

STOMP is derived on top of WebSockets. STOMP just mentions a few specific ways on how the message frames are exchanged between the client and the server using WebSockets. It is a specification to allow asynchronous bidirectional communication between a client and a server.

How do I use WebSockets in Spring boot?

In order to tell Spring to forward client requests to the endpoint , we need to register the handler. Start the application- Go to http://localhost:8080 Click on start new chat it opens the WebSocket connection. Type text in the textbox and click send. On clicking end chat, the WebSocket connection will be closed.


1 Answers

@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

like image 93
kumar Avatar answered Sep 17 '22 07:09

kumar