Creating WebSocket with Spring Boot WebFlux

In this spring webflux websocket example, Learn to create reactive applications using spring webflux which support websocket connection between a client and server.

A websocket is a bi-directional, full-duplex, persistent connection between a web browser and a server. Once the connection is established, it stays open until the client or server decides to close this connection. Websockets have practical use in applications where multiple users connects with each other and send and receive messages e.g. chat applications.

1. Maven Dependencies

We need to have spring-boot-starter-webflux and javax.websocket-api dependencies.

Spring WebFlux expects that WebSockets version 1.1 is used. When using 1.0, the code won’t run.

<project xmlns=""


		<relativePath /> <!-- lookup parent from repository -->







2. WebSocketHandler – Message handler

At the center of the application, we will have a WebSocketHandler which will handle WebSocket messages and lifecycle events. The given EchoHandler will receive a message and return it prefixed with “RECEIVED ON SERVER ::”.

package com.howtodoinjava.demo.handler;

import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.Mono;

public class EchoHandler implements WebSocketHandler 
	public Mono<Void> handle(WebSocketSession session) 
		return session
				.send( session.receive()
						.map(msg -> "RECEIVED ON SERVER :: " + msg.getPayloadAsText())

3. Configure WebSocketHandler

First, WebSocketHandler needs to be mapped to a URL using the SimpleUrlHandlerMapping. Then we need a WebSocketHandlerAdapter which will invoke the WebSocketHandler.

Finally, to let the WebSocketHandlerAdapter understand the incoming reactive runtime request, we need to configure a WebSocketService with the ReactorNettyRequestUpgradeStrategy (as we are using default netty server).

package com.howtodoinjava.demo;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.WebSocketService;
import org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy;

import com.howtodoinjava.demo.handler.EchoHandler;

public class EchoApplication {
	public static void main(String[] args) {, args);

	public EchoHandler echoHandler() {
		return new EchoHandler();

	public HandlerMapping handlerMapping() {
		Map<String, WebSocketHandler> map = new HashMap<>();
		map.put("/echo", echoHandler());

		SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
		return mapping;

	public WebSocketHandlerAdapter handlerAdapter() {
		return new WebSocketHandlerAdapter(webSocketService());

	public WebSocketService webSocketService() {
		return new HandshakeWebSocketService(new ReactorNettyRequestUpgradeStrategy());

4. Websocket client

Let’s create a reactive webclient first. To test it in browser, we have following two files app.js and index.html. The JS file has code to connect/disconnect, send messages and display received messages from server.

var ws = null;
var url = "ws://localhost:8080/echo";

function setConnected(connected) 
	document.getElementById('connect').disabled = connected;
	document.getElementById('disconnect').disabled = !connected;
	document.getElementById('echo').disabled = !connected;

function connect() 
	ws = new WebSocket(url);
	ws.onopen = function() {
		log('Info: Connection Established.');
	ws.onmessage = function(event) {
	ws.onclose = function(event) {
		log('Info: Closing Connection.');

function disconnect() 
	if (ws != null) {
		ws = null;

function echo() 
	if (ws != null) 
		var message = document.getElementById('message').value;
		log('Sent to server :: ' + message);
	} else {
		alert('connection not established, please connect.');

function log(message) 
	var console = document.getElementById('logging');
	var p = document.createElement('p');
<!DOCTYPE html>
<link type="text/css" rel="stylesheet"
	href="" />
<script type="text/javascript" src="app.js"></script>
		<div id="connect-container" class="ui centered grid">
			<div class="row">
				<button id="connect" onclick="connect();" class="ui green button ">Connect</button>
				<button id="disconnect" disabled="disabled" onclick="disconnect();"
					class="ui red button">Disconnect</button>
			<div class="row">
				<textarea id="message" style="width: 350px" class="ui input"
					placeholder="Message to Echo"></textarea>
			<div class="row">
				<button id="echo" onclick="echo();" disabled="disabled"
					class="ui button">Echo message</button>
		<div id="console-container">
			<div id="logging"></div>

5. Test Spring webflux websocket example

Hit the browser with URL : http://localhost:8080/index.html

Test the websocket connect, disconnect feature and then try to send few messages.

Spring webflux + websocket example
Spring webflux + websocket example

Drop me your questions related to creating a reactive websocket connection with server using spring webflux.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “Creating WebSocket with Spring Boot WebFlux”

  1. I have question . In case if all message broadcast through webSocket if i want this message for specifically user . How can i do . Any people help me

  2. Hi, thanks for this clear and very useful example. Could you tell me where to find more info about how to connect two or more sessions in a chat? Thanks a lot.

  3. Thanks great example for beginners. Can you tell how do we add security to webflux websocket so that the client should send url wss://localhost:8080/sample to connect to websocket.


Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.