Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including sse-fetcher with all npm packages installed. Try it out:

var sseFetcher = require("sse-fetcher")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

sse-fetcher v1.0.1

SSE events reimplemented on top of fetch()

SSEFetcher

This a class that reads server sent events using fetch(). It works in Edge (which doesn't support EventSource) if you have a TextDecoder polyfill.

Usage

const sse = new SSEFetcher(url, opts);
// opts.withCredentials - Send cookies to cross-origin URLs.
// opts.reconnectionDelay - Initial reconnection delay.

(async function() {
  while (true) {
    const message = await sse.nextMessage();
    console.log(message);
    // message.data - Data sent
    // message.type - Event type sent
  }
})();

// Later, stop events & close the connection.
sse.close();

Demo: https://sse-fetcher.glitch.me/.

Files

  • lib/index.ts - Original typescript.
  • dist/SSEFetcher.mjs - JS module. Default exports SSEFetcher.
  • dist/SSEFetcher.js - Plain JS. Exposes SSEFetcher on the global.
  • dist/SSEFetcher-min.js - Minified plain JS. 1k gzipped.
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free