My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

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

Alex Blattner's photo
Alex Blattner
·Feb 6, 2017

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!