springspring-websocketstompsockjsamazon-mq

"message:Broker not available." error when implementing stomp over spring websocket


I'm building a sample chat app using spring WebSocket, SockJs and Amazon MQ. It is throwing a 'broker not available' exception when the client subscribes to the topic. All the inbound traffic rules are set correctly in the AWS security groups, and the broker has stomp support too. I'm following this Spring Guide.

It works fine if I'm using the in-memory broker. I really appreciate your help on this, and the following is the sample code.

Broker: Amazon MQ (uses Active MQ internally)

version: 5.15.0

WebSocketConfig.java

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {

    registry.enableStompBrokerRelay("/topic")
            .setRelayHost("***********.mq.us-east-2.amazonaws.com").setRelayPort(61614)
            .setClientLogin("******").setClientPasscode("*****");

    registry.setApplicationDestinationPrefixes("/app");

}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

    registry.addEndpoint("/chat-endpoint").withSockJS();
}

Application Start Log

.......
INFO 14280 --- [alina-utility-1] o.s.m.s.s.StompBrokerRelayMessageHandler : Starting...

INFO 14280 --- [alina-utility-1] o.s.m.s.s.StompBrokerRelayMessageHandler : Starting "system" session, StompBrokerRelay[ReactorNettyTcpClient[reactor.netty.tcp.TcpClientDoOn@7acb7b3e]]

INFO 14280 --- [alina-utility-1] o.s.m.s.s.StompBrokerRelayMessageHandler : Started.
......

Client

var socket = new SockJS('/chat-endpoint');
    stompClient = Stomp.over(socket);

    stompClient.connect({}, function(frame) {

        setConnected(true);
        stompClient.subscribe('/topic/message', function(message) {
                                   displayMessage(message); });

});

Browser Console Log

Opening Web Socket... Web Socket Opened... CONNECT accept-version:1.1,1.0 heart-beat:10000,10000

ERROR message:Broker not available. content-length:0

stomp.min.js:8 Whoops! Lost connection to http://localhost:8080/testApp/chat-endpoint


Solution

  • I had the same problem. To fix it I slightly changed configureMessageBroker method:

        @Override
        public void configureMessageBroker(MessageBrokerRegistry registry) {
            ReactorNettyTcpClient<byte[]> client = new ReactorNettyTcpClient<>(tcpClient -> tcpClient
                    .host("your-amazon-mq-host.amazonaws.com")
                    .port(61614)
                    .secure(SslProvider.defaultClientProvider()), new StompReactorNettyCodec());
    
            registry.setApplicationDestinationPrefixes("/app");
            registry.enableStompBrokerRelay("/queue", "/topic")
                    .setAutoStartup(true)
                    .setSystemLogin("amazonmq-login")
                    .setSystemPasscode("amazonmq-pass")
                    .setClientLogin("amazonmq-login")
                    .setClientPasscode("amazonmq-pass")
                    .setTcpClient(client);
        }