There is not official support for NativeScript in pusher-js sdk, but the Pusher team made a great work designing the JS library to target multiple platforms (react-native, node, browser, etc), while reusing a lot of code in the process. Taking advantage of that architecture, we were able to create a version of the pusher-js sdk that maintains the original API and provides support for NativeScript.
You can install the library using any npm compatible package manager, like npm or yarn:
$ npm install --save pusher-nativescript
Or use NativeScript CLI:
$ tns plugin add pusher-nativescript
Once the package has been installed, you can import it
As a CommonJS module:
const Pusher = require('pusher-nativescript');
Or ES6 module:
import Pusher from 'pusher-nativescript';
If you are using Typescript you might have to import it like this:
import * as Pusher from 'pusher-nativescript';
This build of
pusher-js uses NativeScript's Connectivity API to detect changes on connectivity state. It will use this to automatically reconnect.
On Android, to access any connection related information we will need explicit permission from the user. To enable the permission request add the follwing in
NativeScript do not have official support for WebSockets. We are relying on nativescript-websockets plugin to add that support. That is why the plugin is a peer dependency.
As it was mentioned above,
pusher-nativescript shares the same API as the official
pusher-js library. To see how the library is used and configured checkout pusher-js repository.