Helper function to create an RxJS observable from EventSource
.
npm install rx-event-source rxjs@7
or
yarn add rx-event-source rxjs@7
See available options.
import { eventSource$ } from 'rx-event-source';
const options = {
// ...
};
eventSource$('/api/v1/sse', { withCredentials: false }, options).subscribe((data) => {
console.log(data);
});
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);
});
Thanks goes to these wonderful people (emoji key):
Katarina Anton 💻 🤔 🚧 |
This project follows the all-contributors specification. Contributions of any kind welcome!