简体   繁体   中英

Spingboot Websocket Stomp

Could anyone tell me if the server-side implementation is using stomp WebSocket, is the client also expected to implement stomp?

I am trying to implement a spring boot application and I am confused if I should go with or without stomp implementation. From my research, I understand, if you want to scale the application, it is better to use stomp and embedded broker( RabbitMQ for eg.) as it will handle the sessions, heartbeat etc. instead of an in-memory broker.

The examples available online just shows implementations with and without stomp.

I am basically trying to get different datasets from the table upon client request and write to a WebSocket continuously.

  1. Could anyone please confirm if my understanding so far is correct?
  2. What are the essential things I will have to take care of if I go with stomp + websocket?

Updating the usecase below:

The mobile client would be displaying charts upon user login. There would be links in the left panel for eg. Sales, Discounts etc. which upon clicking, the request will reach server through websocket channel. Server will check the datatype in the request, generate the model using data from DB and write the data to the websocket.

Updating code - v1 MyWebSocketHandler:

@Component
public class MyWebSocketHandler extends TextWebSocketHandler {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    DashboardUtil dashboardutil;

    @Resource(name = "socketSessionsMap")
    private Map<String, WebSocketSession> socketSessionsMap;

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message)
            throws InterruptedException, IOException {

        try {
            //Gets the socket session from map and writes a json to that socket - did for testing purpose.
            socketSessionsMap.put("session", session);
            //String payload = message.getPayload();

            String jsonString = dashboardutil.getDataInJSON(); // gets hardcoded json model
            session.sendMessage(new TextMessage(jsonString));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {

    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {

    }
}

WebSecurityConfig:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
         registry.addHandler(myWebSocketHandler, "/socketHandler").setAllowedOrigins("*").withSockJS();

    }
}

Could anyone tell me if the server-side implementation is using stomp WebSocket, is the client also expected to implement stomp?

You can register multiple handlers in your web socket configuration. So in theory you can provide a handler for STOMP and another one for plain web socket. If you only provide a STOMP handler then the handshake from a standard web socket client will fail.

From my research, I understand, if you want to scale the application, it is better to use stomp and embedded broker( RabbitMQ for eg.) as it will handle the sessions, heartbeat etc. instead of an in-memory broker.

That's correct. STOMP also offers a few more nice features especially the subscription to certain endpoints.

I am basically trying to get different datasets from the table upon client request and write to a WebSocket continuously.

That's a really simple description ... You should think about if you need to share sessions across multiple instances or if you need to send messages between web socket sessions. From your description it sounds like you just accept a web socket connection and continuously push data to the client. If you want to scale this application you can just put a load balancer in front of your instances and you are good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM