{{announcement.body}}
{{announcement.title}}

Server-Sent Events Using Spring

DZone 's Guide to

Server-Sent Events Using Spring

Here we are going to discuss sending unidirectional asynchronous events to any web app using Spring.

· Java Zone ·
Free Resource

Here we are going to discuss sending unidirectional asynchronous events to any web app using Spring. You can send unidirectional events using the SseEmitter class in Spring. There is already a popular solution available for sending bi-directional events using Websockets. Using WebSockets both server and clients can communicate with each other using bi-directional connections between client and server. SSE is only used for sending uni-directional events from the server to clients using the HTTP protocol.

Basic knowledge of Spring and java threading is required.

Steps involved in SSE ar:

  1. The client opens an HTTP connection
  2. The server can send any number of messages to this connection asynchronously
  3. The server can close a connection or it can be closed because of some network error or any exception at the server-side.
  4. In case the connection is closed because of any error from the server or any network error, the client will automatically try to re-connect

Events

The server can send multiple events before closing the connection. Messages sent by the server should be text-based and the message starts with a keyword followed by a colon(:) and then a string message. ‘data’ is a keyword which represents a message for a client.

Java
 




xxxxxxxxxx
1


 
1
data: message1
2
data: message2
3
data: message3
4
data: message4



In the case of multiple messages, messages should be separated by a blank line otherwise client will treat them as a single event. In the above case, all the 4 messages will be treated as a single event. SseEmitter class from package org.springframework.web.servlet.mvc.method.annotation used for emitting messages will take care of this next line and keyword format when you send multiple messages in Spring. You can also send objects in messages as a JSON string, which client can parse

Java
 




xxxxxxxxxx
1


 
1
data:{"message":"message1","userId":"1"}
2
 
           
3
data:{"message":"message2","userId":"2"}
4
 
           
5
data:{"message":"message3","userId":"3"}



Creating Events Using SseEmitter

In Spring you can create an Async controller which will emit multiple messages to the client. You can create a normal GET type controller which can also accept multiple query parameters from the client.

Java
 




xxxxxxxxxx
1
22


 
1
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
2
 
           
3
@GetMapping("/emitter")
4
public SseEmitter eventEmitter(@RequestParam String userId) {
5
   SseEmitter emitter = new SseEmitter(12000); //12000 here is the timeout and it is optional   
6
   
7
   //create a single thread for sending messages asynchronously
8
   ExecutorService executor = Executors.newSingleThreadExecutor();
9
   executor.execute(() -> {
10
       try {
11
           for (int i = 0; i < 4; i++) {
12
              emitter.send("message" + i);           
13
           }       
14
       } catch(Exception e) {
15
            emitter.completeWithError(e);       
16
       } finally {
17
            emitter.complete();       
18
       }   
19
   });
20
   executor.shutdown();
21
   return emitter;
22
}



Here, first, you have to create an object of SseEmitter with an optional timeout as an argument in the constructor.

Here we have created a single thread and send 4 messages from it. You can send multiple messages from multiple different threads as well. You can call multiple APIs in different threads and emit the response of those APIs asynchronously.

In case of error, you can send a special event with a complete error method. This will instruct the client about the error and we will check later how to handle error in the client.

Finally, after sending all the messages, you have to complete emitter by calling a complete method to close the connection.

The output of calling this controller will be multiple messages separated with a blank line so that client will treat them as separate events:

Java
 




xxxxxxxxxx
1


 
1
data: message0
2
 
           
3
data: message1
4
 
           
5
data: message2
6
 
           
7
data: message3 



Client-side Handling of Events

You can handle events in any javascript library or framework like react, angular, etc.

SSE is unidirectional, but the client can also communicate with the server by opening a separate connection with new request params.

For handling events client can open a connection with EventSource object with the URL of server API. EventSource is supported by mostly all of the popular browsers.

const eventSource = new EventSource

('http://localhost:8080/emitter?userId=123'); 

This will open up a connection with a server. You can handle events returned from the server by adding an event listener for an event named “message”. For all the successful events from the server, the callback method which is passed as the second argument while registering event listener will be called with one argument as an event object. Message sent from the server can be retrieved from event object by using property ‘data’, so event.data will hold the message sent by the server. This event.data can be a simple string or a JSON string. If it is a JSON string you have to parse it:

Java
 




xxxxxxxxxx
1


 
1
eventSource.addEventListener("message", (event) => {
2
   const message = JSON.parse(event.data);
3
   console.log(message); 
4
})



There are two other built-in events as well apart from “message” in EventSource, one is ‘open’ which will be called once 200 status is received from the server. As the server is returning emitter without waiting for a thread to complete, the ‘open’ event will not wait for receiving all the messages and it will be called once the server responds with 200 status. Another event is ‘error’, this will be called whenever there is a network error and also when the server closes the connection by calling a ‘complete’ or ‘completeWithError’ method on the emitter.

Java
 




xxxxxxxxxx
1
12


 
1
eventSource.addEventListener("open", (event) => {
2
   console.log('connection is live');
3
});
4
 
           
5
eventSource.addEventListener("error", (event) => {
6
   if (event.readyState == EventSource.CLOSED) {
7
      console.log('connection is closed');
8
   } else {
9
      console.log("Error occured", event);
10
   }
11
   event.target.close();
12
});



So when you call emitter.completeWithError(exception) from the spring controller in case of any exception, then you can catch that event in an “error” event listener. Also, when you call emitter.complete() then too this “error” event listener will be called and console message from the first ‘if’ block will be printed. You can close connection at client-side by calling event.target.close(); , otherwise, the client will keep on retrying connection in case of error.

You can create named events with the help of SseEventBuilder in spring.

Java
 




xxxxxxxxxx
1


 
1
SseEmitter emitter = new SseEmitter();
2
SseEmitter.SseEventBuilder sseEventBuilder = SseEmitter.event()
3
          .id("0") // You can give nay string as id
4
          .name("customEventName")
5
          .data("message1")
6
          .reconnectTime(10000); //reconnect time in millis
7
emitter.send(sseEventBuilder);



The output format for the above message is:

Java
 




xxxxxxxxxx
1


 
1
event: customEventName
2
id: 0
3
data: message1
4
retry: 10000



Named Events

Here, from the spring controller, you are sending an event with a name, so you can create an event listener with the same name to handle messages of this event. This can be useful when you want to send different types of events from the server and the client can handle them differently by registering an event listener corresponding to them.

Java
 




xxxxxxxxxx
1


 
1
eventSource.addEventListener("customEventName", (event)=> {
2
   const message = JSON.parse(event.data);
3
   console.log(message);
4
})



Id

Here, from spring you are sending an id, which can be any string value, you can retrieve this id in your client-side event listener by using lastEventId property of event object. This can be useful to track which events are received successfully by the client. In case of error, the browser will send a special header “Last-Event-ID” in re-connection request (This will only happen if you are not closing connection at client side in your “error” event listener, if you are closing connection then the browser will not retry connection in case of error). The server will parse this header in the request and can decide to decide which message to send next. In case you don’t want to track then there is no need to send id along with the message.

Java
 




x


 
1
eventSource.addEventListener("customEventName", (event)=> {
2
   console.log("Message id is " + event.lastEventId);
3
})



Retry

The browser will keep the server connection open. The server can close the connection by calling ‘complete’ or ‘completeWithError’ method and both of these events are handled at the client-side by “error” event listener. In our example, we are closing connection in our “error” event listener by calling event.target.close() at client side. But, if we don’t a close connection at the client-side then the browser will retry connection in case the connection is closed by the server or by some network error.

By default, the browser will wait for 3s before trying to establish the connection again and the browser will keep on retrying till it gets 200 status from the server. The server can change this default 3s wait time by sending this ‘retry’ flag. Here, in the above example server instructs the browser to wait for 10s (10000 milliseconds) before retrying connection in case of error by sending this “retry” flag. The server can send this flag value 0 as well, which will tell the browser to reconnect immediately if the connection is closed.

That’s all for SSE, hope you will find it useful.

Topics:
event source, events, java, server sent events, spring, sse

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}