Change Server-Sent Event Source (url)

How to change the source set in an EventSource ?

I 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 remains the same ...

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

+3
source share
2 answers

No, It is Immpossible. The new URL passed to EventSource() creates a new EventSource object.

+1
source

guest271314 offered me many solutions, but for me it would be much better, faster and less difficult to stay with sse.

I changed the source by closing and removing (all necessary) the original var.

Here is how I did it:

 source.url = "/blahblah2.php"; source.close(); delete source; //Redefine source 

IMPORTANT: This will not work if you defined the source using var ! Redefinition should also pass without it!

+1
source

Source: https://habr.com/ru/post/1269969/


All Articles