Receiving server-sent events
Watch an event stream as it arrives, filter its events and stop it when you're done.
Overview
Some servers send a response as a stream of server-sent events, with the text/event-stream content type, and keep the connection open to send more. Relay recognizes these responses on its own: you send the request as usual, with any method, body and authentication, and the events appear as they arrive.
While the response streams, a Streaming capsule appears beside the status badge, and the response inspector switches to the Events tab, unless you chose a tab while the request was in progress. Stop replaces Send in the URL bar right away. The Body tab shows the raw body once the stream ends.
Read the events
The Events tab lists each event in a table with its time since the stream opened, its event name, its ID, its data and its size. An event without a name is listed as message.
Select an event to see its data below the table. JSON data is formatted and highlighted. To copy the selected event's data, click the Copy Event button. To give the data more room, drag the bar above it up.
While Follow is on, the table keeps the newest event in view. Scroll up to pause it, or click the Follow button to turn it back on.
The bar at the bottom of the tab shows how many events have arrived, how many bytes, and how long the stream has been open. Once the stream ends, it shows why, such as Closed by server, Stopped or Idle timeout.
Filter the events
- To show only events with one name, click All Events above the table and choose the name. The menu lists the names that have arrived.
- To show only events that contain some text, enter it in the Filter field. Relay matches it against each event's data, name and ID.
Servers often send comments to keep the connection open. Relay hides them unless you click the More button (…) and choose Show Comments.
Copy events
To copy events as newline-delimited JSON, with one event per line, click the More button (…) and choose Copy All as NDJSON. While a filter is active, the item is Copy Shown as NDJSON and copies only the events the table shows.
Stop a stream
Click Stop in the URL bar, or press Command-Period. You can also choose Request > Stop Stream.
Stopping a stream ends it the same way as when the server closes it: Relay keeps the events that arrived, runs the post-response scripts and records the response in History. If you stop a request before its stream opens, Relay cancels it and records nothing.
Let a quiet stream end
When a stream sends nothing for as long as the Streaming Idle Timeout allows, Relay ends it and keeps its events. The timeout is 300 seconds unless you change it in the Requests pane of Relay > Settings, to anything from 1 second to 24 hours. Until the response turns out to be an event stream, the request's own timeout applies.
Understand the limits
While a stream is open, Relay keeps up to 10,000 events and 64 MB of event data. Past that, it drops the earliest events, and the bar at the bottom of the tab shows how many were dropped.
History keeps the newest 2,000 events of each stream, up to 8 MB.
Read events in scripts
Post-response scripts can read a stream's events with relay.response.events(), which returns an array of objects with event, data, id, retry and time properties:
const events = relay.response.events();
test("Stream ends with done", () => {
expect(events[events.length - 1].event).to.equal("done");
});For more information, see Testing responses.
See Also
- Sending, stopping, and replaying requests: Send a request, stop one that's in progress and send a past request again.
- Inspecting responses: Read a response's status, body and headers, and copy or save what you need.
- Testing responses: Check responses automatically with tests and assertions.
- Sending and receiving messages: Send saved text, JSON and binary messages, and read the connection's transcript.