We have seen a popular choice for sending real time data from server to client in web application is using WebSocket in our previous tutorials Messaging with STOMP over WebSockets using Spring and ActiveMQ and Spring Boot WebSocket AngularJS Gradle Example. WebSocket opens bidirectional connections between server and client. So both server and client can send messages. But sometimes we face situations, where the application needs only one way communication, i.e., sending data from server to the client and for this Spring provides a simpler solution using Server Sent Events (SSE). SSE is a technology that allows you to stream data from server to the browser  (Push Notifications) within one HTTP connection in one direction. For example, pushing stock price changes in real-time or showing progress of long-running process or real time showing of cricket or football scores on display board etc.

Browser Support

SSE are supported in most modern browsers. Only Microsoft’s IE and Edge browsers do not have a built in implementation. But there is a way out because Server-Sent Events uses common HTTP connections and can therefore be implemented with the following libraries to support IE and Edge browsers. by Remy Sharp by Rick Waldron by AmvTek by Yaffle


Knowledge of Spring
Knowledge of Java
Have Java 8 installed and configured
Have Gradle 4 installed and configured
Eclipse IDE


In the following example we create a Spring Boot application that sends the random Java’s UUID message with timestamp as SSE to the client. Ideally ou would like to display some meaningful data to the client. So you can always modify the code as per your requirements. The client is a simple html page that displays these values. Spring introduced support for Server Sent Events(SSE) with version 4.2 (Spring Boot 1.3).

First create a Gradle based spring boot project using the below build.gradle script

buildscript {
	ext {
					springBootVersion = '1.5.9.RELEASE'
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

repositories {
dependencies {

Once the project is created and build is done, create below a scheduled service that reads the greeting message every three second and creates an instance of the Notification class and publishes it with Spring’s event bus infrastructure.

package com.jeejava.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

public class NotificationService {

	final DateFormat DATE_FORMATTER = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");

	final List emitters = new CopyOnWriteArrayList<>();

	public void addEmitter(final SseEmitter emitter) {

	public void removeEmitter(final SseEmitter emitter) {

	@Scheduled(fixedRate = 5000)
	public void doNotify() throws IOException {
		List deadEmitters = new ArrayList<>();
		emitters.forEach(emitter -> {
			try {
						.data(DATE_FORMATTER.format(new Date()) + " : " + UUID.randomUUID().toString()));
			} catch (Exception e) {


We are pushing data every 5 secs to the client. For more information on @Async please read Spring Asynchronous Execution using @Async .

Next we create a RestController class that handles the EventSource GET request from the client. The get handler needs to return an instance of the class SseEmitter. Each client connection is represented with its own instance of SseEmitter. Spring does not give you tools to manage these SseEmitter instances. In this application we store the emitters in a simple list(emitters) and add handlers to the emitter’s completion and timeout event to remove them from the list.


import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import com.jeejava.service.NotificationService;

public class NotificationController {

	NotificationService service;

	final List emitters = new CopyOnWriteArrayList<>();

	public ResponseEntity doNotify() throws InterruptedException, IOException {
		final SseEmitter emitter = new SseEmitter();
		emitter.onCompletion(() -> service.removeEmitter(emitter));
		emitter.onTimeout(() -> service.removeEmitter(emitter));
		return new ResponseEntity<>(emitter, HttpStatus.OK);


By default, Spring Boot with the embedded Tomcat server keeps the SSE HTTP connection open for 60 seconds. An application can change that with an entry to the file

spring.mvc.async.request-timeout=-1 #-1 means infinity

We don’t want to start Tomcat server on random port so change the server port at file


Create below main class to start up the application

package com.jeejava.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.jeejava")
public class Application {

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


Client Part

The client opens the SSE connection with

const eventSource = new EventSource('http://localhost:9999/notification');

and registers a message listener that parses the JSON and sets the innerHTML of three dom elements to display the received data.

The whole HTML file is given below

<!DOCTYPE html>
<title>Server Notifications</title>
	function initialize() {
		const eventSource = new EventSource('http://localhost:9999/notification');
		eventSource.onmessage = e => {
			const msg =;
			document.getElementById("greet").innerHTML = msg;
		eventSource.onopen = e => console.log('open');
		eventSource.onerror = e => {
			if (e.readyState == EventSource.CLOSED) {
			else {
		eventSource.addEventListener('second', function(e) {
		}, false);               
	window.onload = initialize;
	<div id="greet"></div>

Testing the application

Now while you run the client file in any modern browsers like Chrome, Firefox etc. then you may face below problem

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource spring boot html

To resolve this issue please write below servlet filter

package com.jeejava.filter;


import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

public class CorsFilter extends OncePerRequestFilter {

	protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
			throws ServletException, IOException {
		res.setHeader("Access-Control-Allow-Origin", "*");
		res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
		res.setHeader("Access-Control-Max-Age", "3600");
		res.addHeader("Access-Control-Expose-Headers", "xsrf-token");
		if ("OPTIONS".equals(req.getMethod())) {
		} else {
			chain.doFilter(req, res);

Now refresh the client file in browser, you will get uninterrupted message being displayed

server sent events with spring

Thanks for reading.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *