16

check my code below, Here I have added three alert messages to each event type, but in this code source.onopen()[alert: readyState: 1] and source.onerror()[alert: readyState: 0] works properly but in case of onmessage() it is not executed.`

       if(typeof(EventSource) !== "undefined") {
           var source = new EventSource('clinic/get');
           source.onopen = function(){
             alert('connection is opened.'+source.readyState);  
           };

           source.onerror = function(){
               alert('error: '+source.readyState);
           };

           source.onmessage = function(datalist){

               alert("message: "+datalist.data);
           };

        } else {
            document.getElementById("clinic-dtls").innerHTML = "Sorry, your browser does not support server-sent events...";
        }`

check the code below for the server side

Random random = new Random();
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        System.out.println("clinic/get got hit");

        try {
            Writer out = response.getWriter();
            out.write("data: welcome data"+random);
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I am using STS(Spring tool Suits), I am wondering, when I am using ctrl+space on EventSource (source) object, in the options it only shows onopen() and onerror(), where is the onmessage().

I will be highly appreciate, if I'd get any response. --Thanks

macken88
  • 125
  • 11
Ram
  • 1,461
  • 1
  • 13
  • 17

3 Answers3

26

I think the correct formatting is:

out.write("event: message\n");
out.write("data:" + value + "\n\n");

The onmessage handler assumes the event name is message. If you want to use other event names, you can subscribe to them using addEventListener.

Pappa
  • 1,593
  • 14
  • 20
  • 1
    This worked for me after hours of head scratching and many tabs open. Specifying the event the client will be listening to. – zeezor Feb 26 '20 at 20:03
  • 2
    Thank you, this was my issue. – darkhipo Aug 05 '21 at 21:57
  • Especially if you are working with polifil event source it's important to set event:message, otherwise eventSource.addEventListener("message", ...) won't work on the front-end side – Danylo Zatorsky Apr 12 '23 at 11:32
22

Solved it !!!

There is no issue with code, the actual issue is when I am writing response to client my response message should look like as below.

PrintWriter out = response.write("data: message"+value+"\n\n");
out.flush(); //don't forget to flush

In my code I was missing the last part "\n\n" in response object so source.onmessage(datalist) in javascript didn't get hit.

Crazy coding..

Ram
  • 1,461
  • 1
  • 13
  • 17
4

please check type of the stream. onMessage will be fired by default if incoming event stream type is "message" That is only the default type. Absent of Default event field in the event-stream causes this issue

For example:

a) Stream.emit("push", "message", { msg: "price Chnage" }); // this works

b) Stream.emit("push", "test", { msg: "price Chnage" }); // this won't

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
sachin rai
  • 41
  • 2
  • You need both things: the double `\n\n` at the end of your `data` and `message` as event name. – lepe Jul 04 '23 at 04:35