- Documentation: https://web.dev/articles/eventsource-basics
- Use case: broadcasting data from server to browsers
- Benefits:
- Easy to understand and implement (only a few lines of code)
- No library is needed
- Can use same HTTP(S) authentication as elsewhere in the app (which can’t be done with websockets)
- Caveat – not supported on some older browsers: https://caniuse.com/#feat=eventsource
- The server sends a stream to the browser. Conceptually, that stream is infinite: the server never closes it, only the client, by breaking the connection.
- If the stream is ever closed by the server (restart etc.), then the client immediately tries to reconnect.
- The server watches a directory and server-sends timestamps when something changes.
- The current timestamp is embedded in each HTML file that is served.
- A script in each HTML file listens to the server-sent events.
- If the embedded timestamp is different from the received data:
location.reload()
- If the embedded timestamp is different from the received data:
Thanks for feedback:
Where should
SimpleReadable.closeSimpleReadable()
be called?I assume inside the
CloseListener
.BTW it looks really nice and awesome lean.