DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reactive Event Streaming Architecture With Kafka, Redis Streams, Spring Boot, and HTTP Server-Sent Events (SSE)
  • Leveraging Salesforce Using a Client Written In Vue.js
  • Evolving Spring Boot APIs to an Event-Driven Mesh
  • End-to-End Event Streaming With Kafka, Spring Boot and AWS SQS/SNS (Production-Ready Code Guide)

Trending

  • Building a Skill-Based Agentic Reviewer with Claude Code: A Practical Guide Using Skills.MD, MCP Servers, Tools, and Tasks
  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Context Is the New Schema
  • Code Quality Had 5 Pillars. AI Broke 3 and Created 2 We Can’t Measure
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot: Server-Sent Events

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.

By 
Mohit Sinha user avatar
Mohit Sinha
·
Sep. 12, 17 · Tutorial
Likes (32)
Comment
Save
Tweet
Share
79.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Dependencies

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 {
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compileOnly('org.projectlombok:lombok')
    ...
}


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.

@Service
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.

Web API

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

@RestController
@RequestMapping("/stock/transaction")
class StockTransactionController {

    @Autowired
    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

{"user":"adam","stock":{"name":"guava","price":32.73},"when":1505049586252}
{"user":"tom","stock":{"name":"infinity","price":39.97},"when":1505049587291}
{"user":"mike","stock":{"name":"mango","price":36.12},"when":1505049588253}
{"user":"mike","stock":{"name":"mango","price":37.93},"when":1505049589253}
{"user":"tom","stock":{"name":"mango","price":39.83},"when":1505049590253}
...


Conclusion

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.

Spring Framework Server-sent events Spring Boot Event

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

Opinions expressed by DZone contributors are their own.

Related

  • Reactive Event Streaming Architecture With Kafka, Redis Streams, Spring Boot, and HTTP Server-Sent Events (SSE)
  • Leveraging Salesforce Using a Client Written In Vue.js
  • Evolving Spring Boot APIs to an Event-Driven Mesh
  • End-to-End Event Streaming With Kafka, Spring Boot and AWS SQS/SNS (Production-Ready Code Guide)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook