Skip to content

Feedback: Streaming #433

Answered by kettanaito
JasperV asked this question in Q&A
Nov 21, 2024 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

Hi, @JasperV 👋

You can find a Server Sent Events example in here: https://alexocallaghan.com/mock-sse-with-msw.

For posterity, here's the usage example:

import { http, HttpResponse } from "msw";

const encoder = new TextEncoder();

const handler = http.get('http://example.com/stream', () => {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue(
        encoder(
          `event:some-event\ndata:some data\n\n`
        )
      );
      controller.close();
    },
  });

  return new HttpResponse(stream, {
    headers: {
      "Content-Type": "text/event-stream",
    }.
  });
});

I am also working on the first-class support for SSE events in mswjs/msw#2299. …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@kettanaito
Comment options

@JasperV
Comment options

@kettanaito
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants