nats.ws React Native / Expo example
nats.ws depends on some APIs not natively available within the React Native JS runtime. We need to use polyfills to fill in these missing APIs.
Install the required packages in your React Native / Expo project using one of the following commands:
yarn:
yarn add @azure/core-asynciterator-polyfill fastestsmallesttextencoderdecoder nats.ws node-libs-react-native react-native-url-polyfill text-encoding-polyfill
npm:
npm install @azure/core-asynciterator-polyfill fastestsmallesttextencoderdecoder nats.ws node-libs-react-native react-native-url-polyfill text-encoding-polyfill
The following needs to be added to the Metro bundler's configuration file to add Node core modules such as stream
to the React Native runtime.
// metro.config.js
module.exports = {
resolver: {
extraNodeModules: require('node-libs-react-native'),
},
// Add more config here if needed
};
Import the polyfills in your app's starting file (App.js
in my case). This ensures the APIs they provide become available globally within your application's JavaScript context.
import 'react-native-url-polyfill/auto';
import 'text-encoding-polyfill';
import '@azure/core-asynciterator-polyfill'
Now you have completed the setup and can start using nats.ws in your React Native / Expo app.
Use the code in App.js
as a reference to build your solution.
- Clone this repo:
git clone https://github.com/thegreatzeus/NATS-WS-React-Native-Example.git
- cd into the
NATS-WS-React-Native-Example
directory:cd NATS-WS-React-Native-Example
- Install dependencies:
yarn install
ornpm install
- Run:
npm start
Special thanks to kindapath, Darapsas, and Byron Ruth for their invaluable contributions.