Search code examples
websocketclientspring-webflux

Spring WebFlux WebSocket client doesn't receive data


I made a reactive app with the following endpoints:

@Bean
public HandlerMapping handlerMapping() {
    Map<String, WebSocketHandler> map = new HashMap<>();
    map.put("/path", new WsReceiverHandler());
    map.put("/out", new WsSenderHandler());
    int order = -1; // before annotated controllers

    return new SimpleUrlHandlerMapping(map, order);
}

I try to connect to it with the following code in another application:

@Bean
public void wsClientHandler(){
    WebSocketClient client = new ReactorNettyWebSocketClient();

    URI url = URI.create("ws://localhost:8080/out");
    client.execute(url, session ->
            session.receive()
                    .doOnNext(System.out::println)
                    .then());
}

It doesn't receive data. However, I can get data from this endpoint in Insomnia and using Pie Socket addon in a web browser.


Solution

  • The following code works:

    @Bean
    public void wsClientHandler() {
        URI url = URI.create("ws://localhost:8080/out");
        ReactorNettyWebSocketClient client = new ReactorNettyWebSocketClient();
        WsReceiverHandler wsReceiverHandler = new WsReceiverHandler();
    
        client.execute(url, wsReceiverHandler).subscribe();
    }