Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close Server-Sent Events events

I want to know how to close a server-sent events via JavaScript. Below is my dummy code:

var ser=new EventSource("path");
ser.onmessage=function(ev){
if(!ev)
   //want to close HERE!!
else
   console.log(ev);
}
like image 335
ahhmarr Avatar asked Apr 05 '13 16:04

ahhmarr


1 Answers

Here is the way that I found worked best for me

var eventSource = new EventSource("path");
eventSource.onerror = eventSourceErrorFunction;
var eventSourceErrorFunction = function(event)
{
        if (event.eventPhase == EventSource.CLOSED) {
            that.eventSource.close();
            console.log("Event Source Closed");
        }  
} 
like image 129
David Robinson Jr. Avatar answered Oct 29 '22 13:10

David Robinson Jr.