Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Funcational Programming and Flow/Reactive Streams Support #445

Open
hantsy opened this issue Jul 22, 2024 · 1 comment
Open

Funcational Programming and Flow/Reactive Streams Support #445

hantsy opened this issue Jul 22, 2024 · 1 comment

Comments

@hantsy
Copy link

hantsy commented Jul 22, 2024

Add new functional programming API using JDK 9 Flow/ReactiveStreams to replace the current annotation-based declarations.

Refer to the Reactive WebSocket implementation in Spring Webflux.

The server side:

public class EchoHandler implements WebSocketHandler {

    public EchoHandler() {
    }

    @Override
    public Mono<Void> handle(WebSocketSession session) {

        return session.send(session.receive()
                .doOnNext(WebSocketMessage::retain)// Use retain() for Reactor Netty
                .map(m -> session.textMessage("received:" + m.getPayloadAsText()))
        );
    }
}

And the client side:

var socketUri = URI.create("ws://localhost:" + port + "/echo");

WebSocketHandler handler = session -> {

    var receiveMono = session.receive()
            .map(WebSocketMessage::getPayloadAsText)
            .log("client receiving::")
            .doOnNext(replayList::add)
            .then();

    var sendMono = session
            .send(
                    Mono.delay(Duration.ofMillis(100)).thenMany(
                            Flux.just("message one", "message two").map(session::textMessage)
                    )
            )
            .doOnSubscribe(subscription -> log.debug("session is open"))
            .doOnTerminate(() -> log.debug("session is closing"))
            .log("client sending::");

    return sendMono.then(receiveMono);
};

this.client.execute(socketUri, handler)
        .doOnTerminate(latch::countDown)
        .subscribe();
@joakime
Copy link
Contributor

joakime commented Jul 22, 2024

What you are proposing can be built on top of the Jakarta WebSocket API.
Some projects have already done that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants