5

New Spring has some WebSocketClient example on Spring documentation.

WebSocketClient client = new ReactorNettyWebSocketClient();
client.execute("ws://localhost:8080/echo"), session -> {... }).blockMillis(5000);

But it is very short and not clear:

  1. How to send a message to the server (subscribe to a channel)?
  2. Then handle incoming stream and emit Flux messages?
  3. Reconnect to the server when the connection is interrupted?

Could some one provide more complex example?

UPD. I tried to do something like:

public Flux<String> getStreaming() {

    WebSocketClient client = new ReactorNettyWebSocketClient();
    EmitterProcessor<String> output = EmitterProcessor.create();
    Flux<String> input = Flux.just("{ event: 'subscribe', channel: 'examplpe' }");

    Mono<Void> sessionMono = client.execute(URI.create("ws://api.example.com/"),
            session -> session
                    .send(input.map(session::textMessage))
                    .thenMany(session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then())
                    .then());

    return output.doOnSubscribe(s -> sessionMono.subscribe());
}

But that returns only one message. Like I didnt get subscription.

Aleksey Kozel
  • 319
  • 1
  • 9
  • 16

2 Answers2

6

I assume you are using an "echo" service. In order to get some messages from the service, you have to push them into the websocket and the service will "echo" them back to you.

In your example code you are writing only a single element to the websocket. As soon as you push more messages into the socket you will get more back.

I adapted the code to connect to ws://echo.websocket.org instead of a local service. When you browse to /stream you see every second a new message appear.

@GetMapping(path = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> getStreaming() throws URISyntaxException {

    Flux<String> input = Flux.<String>generate(sink -> sink.next(String.format("{ message: 'got message', date: '%s' }", new Date())))
        .delayElements(Duration.ofSeconds(1));

    WebSocketClient client = new ReactorNettyWebSocketClient();
    EmitterProcessor<String> output = EmitterProcessor.create();

    Mono<Void> sessionMono = client.execute(URI.create("ws://echo.websocket.org"), session -> session.send(input.map(session::textMessage))
        .thenMany(session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then()).then());

    return output.doOnSubscribe(s -> sessionMono.subscribe());
}

Hope this helps...

Sadeq Dousti
  • 3,346
  • 6
  • 35
  • 53
crixx
  • 111
  • 1
  • 8
  • The `EmitterProcessor` is deprecated, I tried to use `Sinks.Many` to handle the receive message, the client execution is always blocked, https://stackoverflow.com/questions/69992978/blocking-timeout-exception-in-spring-reactive-websocketclient – Hantsy Nov 17 '21 at 05:56
-2

The documentation link above is to the temporary docs from before Spring Framework 5 was released. Currently the reference provides more information about implementing a WebSocketHandler.

Rossen Stoyanchev
  • 4,910
  • 23
  • 26