Skip to content

Stream a response

Before you begin

Plan
Included in Free, Pro and Team. How plans and limits work
Role
Editor or higher.
You need
  • A mock

A stream sends the body you write in chunks at the times you set, so you can see how a client copes with an event stream, a progress feed or a slow download.

The body is exactly what the caller receives, spread over time. Write it whole: for server-sent events, each event followed by a blank line.

data: {"step": 1}
data: {"step": 2}
data: [DONE]
  1. On the project’s Mocks tab, select the mock’s name, and open the response’s tab.

  2. Under Output, select Stream. If the response names no content type, MockHub adds Content-Type: text/event-stream.

  3. In the Stream card, choose Split at:

    Split at A chunk ends
    Each event (blank line) After each blank line, as an event stream
    Each line After each newline, as NDJSON
    Fixed byte count Every Bytes per chunk bytes
  4. Enter Time to first chunk (ms) and Gap between chunks (ms). Variation (%) lets each gap vary by up to that share either way.

  5. To test a dropped connection, enter Cut the connection after (chunks): the connection ends after that many chunks, without the rest.

  6. Write the body under Response body, and select Save mock.

In the configuration document, the same settings are the response’s stream:

{
"statusCode": 200,
"headers": { "Content-Type": "text/event-stream" },
"body": "data: {\"step\": 1}\n\ndata: [DONE]\n\n",
"stream": { "frame": "sse", "firstMs": 400, "gapMs": 40, "jitterPercent": 30, "cutAfter": null }
}

frame is sse, line or bytes (with chunkBytes); firstMs is 0 to 60,000, gapMs 0 to 10,000, jitterPercent 0 to 100 and cutAfter 1 to 100,000 or null. A stream has no delay: the time to the first chunk takes its place.

Try it never paces. Select Preview this response: it lists the chunks with the time each would leave, without the variation, and the whole body under them. Play shows each chunk at its time, and Stop shows them all again. A response that cuts the connection lists only the chunks a caller receives and says where the connection ends.

A request to the mock receives the headers at once and the chunks at their times. The request log says “Streamed 14 chunks over 2.1 s”, and a text/event-stream body opens as Events, Assembled text for a model’s answer, or Raw.

  • Each plan sets the longest a stream may take, from the first chunk to the last; a longer one is played faster to fit, never refused. The limits page lists it per plan.
  • When the stream cannot be paced, the whole body arrives in one piece after the time to the first chunk: on a plan without streams, a body or chunk count past what the gateway paces, or while the workspace or MockHub already holds as many delayed answers as they allow. A stream counts as one of them for its whole length.
  • A stream is a finished response played back. It cannot wait for an event, send on a timer of its own or stay open past the plan’s limit, and there is no WebSocket.
  • An upstream’s answer, in recording or fallback, is never paced: it arrives as the upstream sent it.
  • The request log records the schedule, not the delivery: a caller that left early or a cut connection is still logged as the whole stream.
  • An imported or promoted text/event-stream response starts with a 300 ms first chunk and 30 ms gaps, since a capture records no chunk times.

Report a problem with this page