rn-eventsource-mercure v1.0.6
rn-eventsource-mercure
This package that implements the EventSource web standard using low-level React Native networking primitives.
There are several EventSource
polyfills today, as highlighted in this post on the state of React Native EventSource polyfills, but none of them satisfy the following three goals:
- Don't depend on the Node.js standard library
- The Node.js standard library isn't supported by React Native.
- Don't depend on a native module
- This makes it harder to work with simple Expo-based apps.
- Don't implement with XmlHttpRequest
- Existing polyfills that use XmlHttpRequest are not optimal for streaming sources because they cache the entire stream of data until the request is over.
Thanks to the low-level network primitives exposed in React Native 0.62, it became possible to build this native EventSource
implementation for React Native. See this thread in react-native-community for a longer discussion around the motivations of this implementation.
Usage
Install the package in your React Native project with:
npm install --save rn-eventsource-mercure
To import the library in your project:
const EventSource = require('rn-eventsource-mercure');
Once imported, you can use it like any other EventSource
. See the MDN Documentation for more usage examples.
const ticker = new EventSource('https://www.example.com/stream?token=blah');
ticker.onmessage = (message) => {
console.log(message.data)
}