All Projects → Azure → fetch-event-source

Azure / fetch-event-source

Licence: MIT license
A better API for making Event Source requests, with all the features of fetch()

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to fetch-event-source

Eventsource
The Hoa\Eventsource library.
Stars: ✭ 99 (-17.5%)
Mutual labels:  server-sent-events, eventsource
Php Sse
A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than Websocket, instead of AJAX request.
Stars: ✭ 237 (+97.5%)
Mutual labels:  server-sent-events, eventsource
eventsource ex
Elixir EventSource (Server-Sent Events) client
Stars: ✭ 16 (-86.67%)
Mutual labels:  server-sent-events, eventsource
rust-eventsource-client
Server-sent events (SSE) client implementation for Rust
Stars: ✭ 24 (-80%)
Mutual labels:  server-sent-events, eventsource
Eventsource
EventSource client for Node.js and Browser (polyfill)
Stars: ✭ 541 (+350.83%)
Mutual labels:  server-sent-events, eventsource
okhttp-eventsource
Server-sent events (SSE) client implementation for Java, based on OkHttp: http://javadoc.io/doc/com.launchdarkly/okhttp-eventsource
Stars: ✭ 70 (-41.67%)
Mutual labels:  server-sent-events, eventsource
Demo Spring Sse
'Server-Sent Events (SSE) in Spring 5 with Web MVC and Web Flux' article and source code.
Stars: ✭ 102 (-15%)
Mutual labels:  server-sent-events, eventsource
ruby-eventsource
Server-sent events (SSE) client implementation for Ruby
Stars: ✭ 19 (-84.17%)
Mutual labels:  server-sent-events, eventsource
Jquery Sse
jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill
Stars: ✭ 37 (-69.17%)
Mutual labels:  server-sent-events, eventsource
Aiohttp Sse
Server-sent events support for aiohttp
Stars: ✭ 125 (+4.17%)
Mutual labels:  server-sent-events, eventsource
Facil.io
Your high performance web application C framework
Stars: ✭ 1,393 (+1060.83%)
Mutual labels:  server-sent-events
Libsse Php
It's an easy-to-use, object-orienlated library for Server-Sent Events
Stars: ✭ 150 (+25%)
Mutual labels:  server-sent-events
Restbed
Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Stars: ✭ 1,551 (+1192.5%)
Mutual labels:  server-sent-events
Server Push Hooks
🔥 React hooks for Socket.io, SEE, WebSockets and more to come
Stars: ✭ 176 (+46.67%)
Mutual labels:  server-sent-events
Fuse
Multiplayer Online Standard
Stars: ✭ 68 (-43.33%)
Mutual labels:  server-sent-events
Axway Amplify Streams Js
AMPLIFY Streams Javascript package containing SDK, documentation and sample applications
Stars: ✭ 79 (-34.17%)
Mutual labels:  server-sent-events
go-sse
Fully featured, spec-compliant HTML5 server-sent events library
Stars: ✭ 165 (+37.5%)
Mutual labels:  server-sent-events
Mercure
Server-sent live updates: protocol and reference implementation
Stars: ✭ 2,608 (+2073.33%)
Mutual labels:  server-sent-events
Gossed
Push the standard output of ANY program to browsers as Server Sent Events
Stars: ✭ 138 (+15%)
Mutual labels:  server-sent-events
Lib.aspnetcore.serversentevents
Lib.AspNetCore.ServerSentEvents is a library which provides Server-Sent Events (SSE) support for ASP.NET Core
Stars: ✭ 138 (+15%)
Mutual labels:  server-sent-events

Fetch Event Source

This package provides a better API for making Event Source requests - also known as server-sent events - with all the features available in the Fetch API.

The default browser EventSource API imposes several restrictions on the type of request you're allowed to make: the only parameters you're allowed to pass in are the url and withCredentials, so:

  • You cannot pass in a request body: you have to encode all the information necessary to execute the request inside the URL, which is limited to 2000 characters in most browsers.
  • You cannot pass in custom request headers
  • You can only make GET requests - there is no way to specify another method.
  • If the connection is cut, you don't have any control over the retry strategy: the browser will silently retry for you a few times and then stop, which is not good enough for any sort of robust application.

This library provides an alternate interface for consuming server-sent events, based on the Fetch API. It is fully compatible with the Event Stream format, so if you already have a server emitting these events, you can consume it just like before. However, you now have greater control over the request and response so:

  • You can use any request method/headers/body, plus all the other functionality exposed by fetch(). You can even provide an alternate fetch() implementation, if the default browser implementation doesn't work for you.
  • You have access to the response object if you want to do some custom validation/processing before parsing the event source. This is useful in case you have API gateways (like nginx) in front of your application server: if the gateway returns an error, you might want to handle it correctly.
  • If the connection gets cut or an error occurs, you have full control over the retry strategy.

In addition, this library also plugs into the browser's Page Visibility API so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unnecessarily (but you can opt out of this behavior if you want.)

Install

npm install @microsoft/fetch-event-source

Usage

// BEFORE:
const sse = new EventSource('/api/sse');
sse.onmessage = (ev) => {
    console.log(ev.data);
};

// AFTER:
import { fetchEventSource } from '@microsoft/fetch-event-source';

await fetchEventSource('/api/sse', {
    onmessage(ev) {
        console.log(ev.data);
    }
});

You can pass in all the other parameters exposed by the default fetch API, for example:

const ctrl = new AbortController();
fetchEventSource('/api/sse', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        foo: 'bar'
    }),
    signal: ctrl.signal,
});

You can add better error handling, for example:

class RetriableError extends Error { }
class FatalError extends Error { }

fetchEventSource('/api/sse', {
    async onopen(response) {
        if (response.ok && response.headers.get('content-type') === EventStreamContentType) {
            return; // everything's good
        } else if (response.status >= 400 && response.status < 500 && response.status !== 429) {
            // client-side errors are usually non-retriable:
            throw new FatalError();
        } else {
            throw new RetriableError();
        }
    },
    onmessage(msg) {
        // if the server emits an error message, throw an exception
        // so it gets handled by the onerror callback below:
        if (msg.event === 'FatalError') {
            throw new FatalError(msg.data);
        }
    },
    onclose() {
        // if the server closes the connection unexpectedly, retry:
        throw new RetriableError();
    },
    onerror(err) {
        if (err instanceof FatalError) {
            throw err; // rethrow to stop the operation
        } else {
            // do nothing to automatically retry. You can also
            // return a specific retry interval here.
        }
    }
});

Compatibility

This library is written in typescript and targets ES2017 features supported by all evergreen browsers (Chrome, Firefox, Safari, Edge.) You might need to polyfill TextDecoder for old Edge (versions < 79), though:

require('fast-text-encoding');

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].