onmessage in Server Side Events is not working. Can someone please help?

Please take a look at the following code:

function connect(){
    if(window.EventSource){
        alert(1);
        var source = new EventSource("geters/notifications.php");
        alert(2);
        source.onmessage= function(event){
            alert(3);
            if(event.data != ""){
                alert(4);
                alert(event.data);
            }
        };
    } else {
        alert("event source does not work in this browser, author a fallback technology");
    }
}

alert(3) didn't show up!

Marco Alka's photo

Are you sure about your spelling? Shouldn't

var source = new EventSource("geters/notifications.php");

rather be

var source = new EventSource("getters/notifications.php"); ("getters" with two t) ?

Hieu Lam's photo

I don't think on the server side env you can use window.eventSource.

n4j1Br4ch1D's photo

Hi "Peace Be Upon You", <br /> concerning 'onmessage in Server Side Events', i had the same problem too, But i found out that problem is with Server side code not client. <br /> Take a look at this simple demo i have made:<br /> github.com/n4j1Br4ch1D/onmessage-in-Server-.. <br /> I hope it solves Your Problem.