Spring Boot: Server-Sent Events

DZone 's Guide to

Spring Boot: Server-Sent Events

Spring 5 will see support for asynchronous and Reactive apps. Here's how to implement one new tool: server-sent events in your apps.

· Java Zone ·
Free Resource

Spring 5, which will release later this year, will support building asynchronous and Reactive applications. To learn more about the features in Spring 5 and Reactive programming, you can refer to this article. I'll highly suggest you do that before continuing on.

Spring 5 will make it easier to implement Server-Sent Events (SSE), and we'll see, with the help of a simple stock market simulation, how to implement SSE.

Server-Sent Events (SSE)

SSE is a web technology where a browser receives updates from a server via an HTTP connection. It is better than polling because polling has a lot of HTTP overhead. Unlike WebSockets, however, it is unidirectional (server to browser).

SSEs are sent over traditional HTTP, hence they don't require any special implementation on the server.


We'll use Gradle to build our project. I recommend using Spring Initializr to bootstrap your project.

We'll use:

  • Spring Boot 2
  • Spring Webflux
  • Lombok

Not all the Spring libraries have a stable release yet.

buildscript {
    ext {
        springBootVersion = '2.0.0.BUILD-SNAPSHOT'

dependencies {

Stock Market Example

In this example, we'll simulate stock market transactions and notify the client about them.

The steps involved are:

  • Initialize stocks with their prices
  • After every second, update stock prices and make stock transactions.
  • The client will receive the details of the stock transactions after every second.

Let's have a look at the Stock class.

class Stock {
    String name;
    float price;

We'll initialize some random Stocks at the start of the application.

Let's have a look at the StockTransaction class.

class StockTransaction {
    String user;
    Stock stock;
    Date when;

Reactive SSE

We'll create a service that returns a Flux of StockTransactions.

class StockTransactionService {
    Flux<StockTransaction> getStockTransactions() {
        Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
        interval.subscribe((i) -> stockList.forEach(stock -> stock.setPrice(changePrice(stock.getPrice()))));

        Flux<StockTransaction> stockTransactionFlux = Flux.fromStream(Stream.generate(() -> new StockTransaction(getRandomUser(), getRandomStock(), new Date())));
        return Flux.zip(interval, stockTransactionFlux).map(Tuple2::getT2);

We are creating a Flux that generates a long value every second. We are then updating the price of every stock. We are creating another Flux that creates a StockTransaction. The Flux.zip method is taking both these Fluxes and combining them. They'll be combined in a strict sequence (when both Fluxes have emitted their nth item). We are then returning the second Flux. Hence, the resulting Flux will emit a StockTransaction after every second.


We'll create an endpoint GET /stock/transaction that will continuously fetch details of the latest transactions happening.

class StockTransactionController {

    StockTransactionService stockTransactionService;

    @GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Flux<StockTransaction> stockTransactionEvents(){
        return stockTransactionService.getStockTransactions();

MediaType.APPLICATION_STREAM_JSON_VALUE signifies that the server will send SSEs.

This API will return a response with the header Content-Type: application/stream+json.

The cURL command for testing this is:

curl -v http://localhost:8080/stock/transaction.

The output obtained will look like this



I have tried explaining, with a simple example, how to send SSEs using Spring Boot. For more information, read up about Project Reactor.

You can find the complete example on GitHub.

java ,reactor core ,server-sent events ,spring 5 ,spring boot ,spring reactive ,spring webflux ,tutorial

Published at DZone with permission of Mohit Sinha , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}