This package that implements the EventSource web standard using low-level React Native networking primitives.
There are several EventSource
polyfills today, 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.
Install the package in your React Native project with:
npm install --save rn-eventsource
To import the library in your project:
const EventSource = require('rn-eventsource');
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)
}