Add EventSource.fromReadableStream()#12272
Conversation
Allows developers use any ReadableStream source, such as from the fetch() API, while using EventSource for event-stream parsing. This separates networking concerns from parsing, enabling use cases like custom authentication or other headers, and allows application developers to handle their own reconnection behaviour. The lastEventId and reconnectionTime getters are added for use by applications in their own reconnection flows. The about:event-stream URL is defined for use as the URL for EventSource instances created from a ReadableStream.
|
These are some of the design decisions I made while writing this spec. Where possible, I preferred to keep the changes as simple for implementers as possible, reusing existing algorithms, with the goal of making it as easy as possible to be implemented. Defining Since there can't be any automatic reconnection and streams can't be reopened, it was easiest to have the connection fail when the stream ends. However, the custom reconnection use case can be handled by the application layer by using a Since the The The I considered different options for what this should return prior to the server sending its own I considered what to do with the Invoking |
|
There's one open issue with this design that would need to be addressed in some way. With traditional EventSource, when the connection breaks, the interpretation of the stream causes incomplete buffers to be discarded before the connection is re-established. But with |
The reset() method allows applications using EventSources created from readable streams to signal that any data in the buffer that has not been dispatched should be discarded, allowing for a fresh stream of event data to be piped in.
Allows developers use any
ReadableStreamsource, such as from thefetch()API, while usingEventSourcefor event-stream parsing.This separates networking concerns from parsing, enabling use cases like custom authentication or other headers, and allows application developers to handle their own reconnection behaviour.
The
lastEventIdandreconnectionTimegetters are added for use by applications in their own reconnection flows.The
about:event-streamURL is defined for use as the URL forEventSourceinstances created from aReadableStream.The
reset()method allows applications to tell EventSource to discard unused data in the buffer to prepare for a fresh stream of event data to be piped in.Fixes #2177
(See WHATWG Working Mode: Changes for more details.)
/infrastructure.html ( diff )
/references.html ( diff )
/server-sent-events.html ( diff )
/urls-and-fetching.html ( diff )