livereload / livereload-js

LiveReload JavaScript code that communicates with the server and implements reloading
http://livereload.com/
MIT License
830 stars 190 forks source link

Support for Server-Sent Events (SSE) #87

Open apla opened 5 years ago

apla commented 5 years ago

For our project we would like to use SSE. Currently, code to activate SSE for LiveReload 3.0.0 is quite simple:


if (LiveReload) {
    LiveReload.connector.disconnect();
}

var source = new EventSource(url);

source.addEventListener ('reload', function (evt) {
    var files;
    try { files = JSON.parse (evt.data); } catch (e) { return; }

    if (LiveReload) {
        files.forEach (function (file) {
            LiveReload.performReload ({path: file})
        });
    } else {
        location.reload ();
    }
});

source.addEventListener ('alert', function (evt) {
    console.log ('alert event', evt);
    if (LiveReload) {
        LiveReload.performAlert (evt.data);
    }
});

Unfortunately, this not works with minified version nor version 3.0.1. I would like to make it works without LiveReload.connector.disconnect() if that's fine for you

smhg commented 5 years ago

I'm not very up-to-date with this. This would function as a replacement for websockets, right? If so, how would you want to signal you want to use SSE instead? Or would livereload 'listen' to both at the same time?

Unfortunately, this not works with minified version nor version 3.0.1.

You mean this doesn't work with something that happened with 3.x (as in: it did work with 2.x)? Or what is problematic with version 3.0.1 and/or minification?

I would like to make it works without LiveReload.connector.disconnect() if that's fine for you

You mean you currently need the first 3 lines to stop the websocket? In that case, it is connected to my first question I guess?

apla commented 5 years ago

I'm not very up-to-date with this. This would function as a replacement for websockets, right?

If so, how would you want to signal you want to use SSE instead? Or would livereload 'listen' to both at the same time?

Actually, I'm preparing the pull request for this. As a LiveReload user, you can choose between WebSockets and EventSource (SSE). WebSockets will be default choice.

From my point of view WebSocket is just a transport to send reload and alert commands. SSE will be additional transport to send those events. I'm planning to add transport property to the Options object and in case transport = sse, different connector will start and emit reload or alert events. Options already contains host, port and path, so transport will be enough.

You mean this doesn't work with something that happened with 3.x (as in: it did work with 2.x)? Or what is problematic with version 3.0.1 and/or minification?

Not sure about problem in minified version, but I've fixed issue with 3.0.1 in #88

You mean you currently need the first 3 lines to stop the websocket? In that case, it is connected to my first question I guess?

With my patch, disconnect() will not be needed anymore, so no problem with minified version.

smhg commented 5 years ago

I'm not fully comprehending what the issues with the minified version are, but if you can/could solve them: awesome.

The idea of multiple transports sounds nice! Looking forward to your PR! Thanks!