user5147563
user5147563

Reputation:

Change source (url) of Server-Sent event

How can I change the source set in declaration of EventSource?

I have tried something like this:

var source = new EventSource("/blahblah.php?path=" + (window.location.pathname));
// Few lines later...
source.url = "/blahblah.php?path=" + url;

But, source.url stays the same...

Is this even possible? Or maybe there are alternative ways to do that?

Upvotes: 6

Views: 2822

Answers (2)

TikasMan
TikasMan

Reputation: 91

I Do it like this.

var source;

function ChangeEventSource(Url) {

    if (source != undefined) {source.close(); }
    
    if (typeof (EventSource) !== "undefined") {
    
        source = new EventSource(Url);
        source.onmessage = function (event) { 
                //Do something here
        };
        
    } else {
        console.log("Sorry, your browser does not support server-sent events...");
    }
}

Upvotes: 0

guest271314
guest271314

Reputation: 1

No, it is not possible. A new URL passed to EventSource() creates a new EventSource object.

Upvotes: 5

Related Questions