'Is is possible to send message using WebSocket after rest called in spring boot?

Is it possible to send messages using WebSocket after rest called in spring boot? Please help correct me, I have some configure as below

//Use to handle connection and text message
Class SignalHandler extent TextWebSocketHandler{

}

@Configuration
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {
    private final SignalHandler signalHandler;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(signalHandler, "/signal")
                .setAllowedOrigins("*");
    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        return container;
    }
}

Currently, it's working fine, if I create a WebSocket connection from Web or mobile by point to http://localhost:8080/signal to join/leave/message which handles on SignalHandler.

In my case, I have a rest request on backend, after success called, I would like to send message to all client who are in room.

Please you advice me, how can I do this?

Thank you.



Solution 1:[1]

What I would suggest here is to use STOMP (https://spring.io/guides/gs/messaging-stomp-websocket/) , which is an application level protocol over the raw web sockets. It gives abstraction as queues and topics which are easier to use. Coming to your question, I think you might want to redesign your apis to use websocket completely. For e.g. you can use topic to broadcast your message whenever somebody joins.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sj221