In Spring WebFlux, WebSocketHandler is the interface that handles a WebSocket connection reactively. Its single method, handle(WebSocketSession session), receives the session and returns a Mono<Void> that completes when the application has finished handling the connection. You read incoming messages from session.receive() and write outgoing ones with session.send(...), both as reactive streams.
The interface lives in org.springframework.web.reactive.socket. It is separate from the servlet-based org.springframework.web.socket.WebSocketHandler used in Spring MVC, which is covered in a different article.
The interface is intentionally small:
handle is called once per connection after the handshake. The returned Mono<Void> represents the whole lifetime of your processing. When it completes, the framework closes the session. When it errors, the session is closed with an error status. getSubProtocols() lets you declare supported sub-protocols such as graphql-transport-ws.
The WebSocketSession gives you the handshake information, a receive() method returning Flux<WebSocketMessage>, a send(Publisher<WebSocketMessage>) method returning Mono<Void>, and factory methods such as textMessage(String) and binaryMessage(...).
This handler echoes each text message back with a prefix:
receive() is a cold stream of inbound messages that ends when the client closes the connection. Mapping it to outgoing messages and passing the result to send ties both directions together. The Mono from send completes when the inbound stream ends, so the session stays open exactly as long as the client does.
Do not subscribe manually inside handle. Return the composed Mono so the framework controls subscription and cancellation.
In WebFlux, you map handlers to paths with a HandlerMapping and add a WebSocketHandlerAdapter. Spring Boot does not register the adapter for you in every setup, so declare both:
A negative order makes the mapping win over annotated controllers. Clients connect with ws://host:8080/ws/echo, or wss:// over TLS.
Because each connection has its own handle call, broadcasting needs shared state. A Sinks.Many from Reactor is the usual tool:
The input pipeline pushes each received message into the sink. The output pipeline subscribes the session to the sink, so every connected client receives every message. Mono.zip completes when either side finishes, which closes the session. directBestEffort drops messages for slow subscribers instead of blocking the others.
send only requests messages as the network can accept them. Avoid unbounded buffers such as onBackpressureBuffer() with no limit.onErrorResume or doOnError on the inbound flux, since an unhandled error closes the connection.session.close(CloseStatus.NORMAL).Schedulers.boundedElastic() through subscribeOn.SecurityWebFilterChain, and session.getHandshakeInfo().getPrincipal() gives you the user.Origin header or configure allowed origins through HandshakeWebSocketService to reduce cross-site WebSocket hijacking risk.Spring provides ReactorNettyWebSocketClient, which connects to a running server in tests:
Always use a timeout with block in tests so a broken handler fails the build instead of hanging it.
A Mono<Void> that completes when you are done with the session. Completion closes the connection.
No. Spring MVC uses org.springframework.web.socket.WebSocketHandler with blocking callbacks such as handleTextMessage. WebFlux uses the reactive interface in org.springframework.web.reactive.socket.
Keep a map of session ids to sinks, one Sinks.Many per connection, and emit to the sink for that id.
Spring WebFlux has no built-in STOMP broker support. Use raw WebSockets, RSocket, or server-sent events, depending on the need.
WebSocketHandler is the reactive entry point for WebSocket connections in Spring WebFlux. Implement handle, compose session.receive() and session.send(...) into one Mono<Void>, and register the handler with a SimpleUrlHandlerMapping and a WebSocketHandlerAdapter. Use Reactor sinks for broadcasting, avoid blocking, and secure the handshake through the normal security filter chain.