Zhichao
Zhichao

Reputation: 619

SockJS receive stomp messages from spring websocket out of order

I am trying to streaming time series data using Springframework SimpMessagingTemplate (default Stomp implementation) to broadcast messages to a topic that the SockJS client subscribed to. However, the messages is received out of order. The server is single thread and messages are sent in ascending order by their timestamps. The client somehow received the messages out of the order.

I am using the latest release version of both stompjs and springframework (4.1.6 release).

Upvotes: 10

Views: 5007

Answers (4)

dgzz
dgzz

Reputation: 2999

UPDATE

Please see @Jason's answer. Spring 5.1 has a setPreservePublishOrder() to order the messages based on their client ID.


I experienced this issue as well. I don't like to limit my thread pool size to 1 for this will cause an overhead on my application. Instead, I used a StripedExecutorService to process messages coming in and out of my application. This type of executor service guarantees ordered processing of messages for tasks that have same stripe. For me, I use WebSocket session ID as stripe. Register this executor via ChannelRegistration.taskExecutor() on your inbound, broker, and outbound channel and this will guarantee ordered messages. Choose your stripe wisely.

Upvotes: 4

Jason
Jason

Reputation: 606

looks like there is a built in striped executor, so just enable it:

@Override
protected void configureMessageBroker(MessageBrokerRegistry registry) {
    // ...
    registry.setPreservePublishOrder(true);
}

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket-stomp-ordered-messages

Upvotes: 13

Dawid Kunert
Dawid Kunert

Reputation: 193

It's Spring web socket design problem. To receive messages in valid order you have to set corePoolSize of websocket clients to 1.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketMessageBrokerConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureClientOutboundChannel(ChannelRegistration registration) {
        registration.taskExecutor().corePoolSize(1);
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.taskExecutor().corePoolSize(1);
    }
}

Upvotes: 10

Zhichao
Zhichao

Reputation: 619

Found the root cause of this issue. The messages were sending in "correct" order from the application implementation perspective (I.e, convertAndSend() are called in one thread or at least thread safe fashion"). However, Springframework web socket uses reactor-tcp implementation which will process the messages on clientOutboundChannel from the thread pool. Thus the messages can be written to the tcp socket in different order that they are arrived. When I configured the web socket to limit 1 thread for the clientOutboundChannel, the order is preserved.

This problem is not in the SocketJS but a limitation of current Spring web socket design.

Upvotes: 10

Related Questions