How to create event-source server using spring boot application? From the below link, I don't see Java-spring boot example. Any reference would help.
Asked
Active
Viewed 2,973 times
1
-
Possible duplicate of [SSE implementation in Spring REST](http://stackoverflow.com/questions/31229015/sse-implementation-in-spring-rest) – kaliatech Sep 20 '16 at 11:27
-
1There was no spring boot solution in that link. It says there is no support currently. So would request to leave this question. – Anand Sep 20 '16 at 12:23
2 Answers
2
This is a simple implementation with Spring 4.2. Don't take into account the Thread, it's there only for demo purposes:
@RestController
public class AccountsRestController {
@RequestMapping("/accounts/alerts")
public SseEmitter getAccountAlertsNoPathVariable(HttpSession session) {
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
Thread t1 = new Thread(() ->{
try {
int i = 0;
// Send 10000 messages
while(++i<=10000){
Thread.sleep(1000);
System.out.println("Sending");
try{
emitter.send(new Alert((long)i, "Alert message"+i));
}catch(ClientAbortException cae){
//The client is not there anymore, we get out of the loop
i = 10000;
}
}
emitter.complete();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
return emitter;
}

codependent
- 23,193
- 31
- 166
- 308
-
1My need is to send the notification count. Just a integer. If I replace the whole thread block with "emitter.send(notificationCount);" how frequently will this code be running? What I'm not getting is, how to stream infinitely until users browser session is active. Should I keep a while loop checking users session and sleep inside the loop ? – Anand Sep 20 '16 at 12:31
-
2if you remove the thread, only one event will be sent. That's why you need a thread to run until you want. As a cleaner alternative you can use a ExecutorService to launch the task that send()s the messages when you want. – codependent Sep 20 '16 at 12:36
-
1To clear things up a little more: you don't need to check if the browser is active. `emitter.send()` will throw an `IOException` (`ClientAbortException` in Tomcat) signalling that the client is not there anymore. I've updated the answer. – codependent Sep 20 '16 at 12:45
-
That exception thing clears what I was exactly wondering at. I'll try and update. Thank you so much :) – Anand Sep 20 '16 at 12:50
-
1Can you please see the code in below answer. Do you see why its not detecting the browser close event? – Anand Sep 20 '16 at 14:07
0
I've tried with the below code, rewritten from @codependent 's solution to suit my need. Its responding. But never terminating the connection when the browser tab closes. Its keep running in the server side. Anything to do with HTTP GET here?
@RequestMapping(value = "/getNotificationCount/{userId}",method = RequestMethod.GET)
public SseEmitter getNotificationCount(@PathVariable("userId") String userId, HttpServletResponse response){
SseEmitter emitter = null;
try {
emitter = new SseEmitter();
while(true) {
try{
int count= myService.getNotificationCount(Integer.parseInt(userId));
emitter.send(count);
Thread.sleep(30 * // minutes to sleep
60 * // seconds to a minute
1000); // milliseconds to a second
}catch(ClientAbortException cae){
LOGGER.info("ClientAbortException Breaking the notification stream");
break;
}
}
//Closes the stream
emitter.complete();
} catch (Exception e) {
//Closes the stream
emitter.complete();
}
return emitter;
}

Anand
- 9,672
- 4
- 55
- 75
-
1It looks OK. Have a look at my sample project: https://github.com/codependent/spring4-sse The endpoint is /accounts/alerts. When I close the browser the exception is raised. – codependent Sep 20 '16 at 14:18
-
1@codependent Found the issue. Its not with the code.. but with the firewall, which is waiting till the connection is closed before it releases the data and connection open event is triggered (Only after the connection is closed in the server side, 'onopen' event is triggered and 'onmessage' is triggered for all messages in one go. Thanks for all the help. Now looking for options for this :( http://stackoverflow.com/a/13135995/1057093 – Anand Sep 21 '16 at 10:25
-