Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server sent events + Java with Spring MVC

I am currently having a problem related to SSE and Windows XP. The source code below is currently working in every Chrome I tried except for Chrome in Windows XP (?) Not sure why. This is intended to be used for a control panel, where users must use Chrome. In other words, I don't care about IE, Firefox, etc.

The problem: Server side events works everywhere (Chrome) but not in Windows XP (Chrome). When I say it works, I mean that the message handler is called.

The code

  • Javascript code

    if (!!window.EventSource) {
       console.log("Event source available");
       var source = new EventSource('/admin/systemalert');
    
       source.addEventListener('message', function(e) {
            console.log(e.data);
       });
    
       source.addEventListener('open', function(e) {
            console.log("Connection was opened.");
       }, false);
    
       source.addEventListener('error', function(e) {
            if (e.readyState == EventSource.CLOSED) {
                console.log("Connection was closed.");
            } else {
                console.log(e.readyState);    <-- in windows XP it prints Error here
            }
       }, false);
    } else {
            console.log("No SSE available");
    }
    
  • Server side code

    @Controller
    @RequestMapping("/admin/**")
    public class AdminController {
    
            @RequestMapping("systemalert")
            public @ResponseBody String sendMessage(HttpServletResponse response) {
                    Random r = new Random();
                    response.setContentType("text/event-stream");
                    try {
                            Thread.sleep(10000);
                    } catch (InterruptedException e) {
                            e.printStackTrace();
                    }   
                    return "data:Testing 1,2,3" + r.nextInt() +"\n";
            }
    
    }
    

As stated in the code, the line console.log(e.readyState); prints "Error" when using Chrome in Windows XP. Any ideas? Anyone see anything wrong with the source code?

Thanks in advance. Agustin

like image 566
Agustin Lopez Avatar asked Apr 09 '12 20:04

Agustin Lopez


People also ask

What is Server-Sent-Events in spring boot?

Simply put, Server-Sent-Events, or SSE for short, is an HTTP standard that allows a web application to handle a unidirectional event stream and receive updates whenever server emits data. Spring 4.2 version already supported it, but starting with Spring 5, we now have a more idiomatic and convenient way to handle it.

What are the events available for Server-Sent-Events?

A server-sent event is when a web page automatically gets updates from a server. This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

What is the difference between Server-Sent-Events SSEs and WebSockets in html5?

Differences. Obviously, the major difference between WebSockets and Server-Sent Events is that WebSockets are bidirectional (allowing communication between the client and the server) while SSEs are mono-directional (only allowing the client to receive data from the server).

How do SSE events work?

SSE is designed to use the JavaScript EventSource API in order to subscribe to a stream of data in any popular browser. Through this interface a client requests a particular URL in order to receive an event stream. SSE is commonly used to send message updates or continuous data streams to a browser client.


2 Answers

Instead of implementing manually SSE, be aware that Spring Framework 4.2+ supports natively SSE. See this sample SSE enabled controller returning an SseEmitter.

like image 27
Sébastien Deleuze Avatar answered Oct 22 '22 13:10

Sébastien Deleuze


For anyone with this problem, the problem was related to the new lines needed after the data. Basically, you need two lines and not one as I was using. That way it works everywhere.

Changing this:

return "data:Testing 1,2,3" + r.nextInt() +"\n";

To this:

return "data:Testing 1,2,3" + r.nextInt() +"\n\n";

Fixes the problem..

like image 78
Agustin Lopez Avatar answered Oct 22 '22 12:10

Agustin Lopez