<invoke type="event-stream">
The event-stream connection type uses the EventSource's event-stream protocol to receive events from a Web server over a persistent HTTP connection. It is a one-way stream: sending information back to the server must be done through another channel, such as fetch
.
While limited, it has the advantage of working over HTTP, it is standard, simple and robust, and fills an important role in client-side software.
The full URI of the type should be "http://www.jsscxml.org/event-stream/" and the short-hand name used in JSSC is "event-stream".
Any URI supported by the underlying EventSource implementation. In Web browsers, that means any HTTP URL (of course, it will still fail if the server doesn't reply with an event-stream at that location).
The event: descriptor must not be used in the event-stream to indicate the name of events, since the EventSource wrapper is a DOM EventTarget interface and it would be silly to implement another one just to be able to react to all events. Instead, events are transmitted as default-named messages with data: lines, the first of which sets the event name (after trimming any white space). If more lines are present, they must contain a single JSON-like representation of the event data:
data: event.name data:{ data: var1: 1, data: var2: /regex/ data:}
Note that the data in the above example is not JSON because the property names are unquoted and one of the values is a litteral RegExp. The only requirement in this specification is that implementations must support basic JSON and fall back to plain text. However, they may support better JavaScript serializations.
On an implementation that only supports basic JSON, the resulting event will be:
The data field contains an unparsed string because the basic JSON parser failed. A platform with a better parser would put the intended object there instead.
The connection terminates in all the usual conditions for the EventSource interface; also, if it receives an event that matches "done.*", it will terminate after dispatching that event.
No payload nor custom headers may be sent to the target for initialization, so only param
s or content
with an object value, and finalize
are allowed. Note that an empty finalize
will not work if the received event data cannot be parsed as an object.