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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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
  • Server-Sent Events Using Spring
  • Sprinkle Some ELK on Your Spring Boot Logs

Trending

  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Ethical AI in Agile
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  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.0K 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, DZone MVB. 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
  • Server-Sent Events Using Spring
  • Sprinkle Some ELK on Your Spring Boot Logs

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!