Skip to content

kaciakmaciak/rx-event-source

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RxEventSource

GitHub release (latest SemVer) All Contributors Gitpod Ready-to-Code codecov GitHub Workflow Status Snyk Vulnerabilities for GitHub Repo Semantic release npm bundle size GitHub

API Reference

Open in Gitpod

Background

Helper function to create an RxJS observable from EventSource.

Installation

NPM

npm install rx-event-source rxjs@7

or

yarn add rx-event-source rxjs@7

Usage

Basic usage

See available options.

import { eventSource$ } from 'rx-event-source';

const options = {
  // ...
};

eventSource$('/api/v1/sse', { withCredentials: false }, options).subscribe((data) => {
  console.log(data);
});

Handling streaming errors

The streaming error can be emitted as a message with specific data format and then handled manually by throwing an error.

import { eventSource$ } from 'rx-event-source';

const options = {
  // ...
};

function isError(data) {
  // ...
}

eventSource$('/api/v1/sse', { withCredentials: false }, options).pipe(
  map((data) => {
    if (isError(data)) {
      throw new Error(data.error);
    }
    return data;
  })
).subscribe((data) => {
  console.log(data);
});

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Katarina Anton

💻 🤔 🚧 ⚠️ 🔧 🚇

This project follows the all-contributors specification. Contributions of any kind welcome!