Pubnub Mobx
Peer Dependencies
This library relies on having mobx and pubnub installed. If you haven't installed them previously :
yarn add pubnub mobx # Or npm i pubnub mobx
Install
yarn add pubnub-mobx # Or npm i pubnub-mobx
Usage
pubnub-mobx
exposes only one function :
init
// Optional;; ;
Listen to data change
;// ... Code from previous sectionobservelisteners.message,;
Map events to your own data structure
;// ... Code from previous section;
Publish a new message
To publish a new message just set
it in publishers.message
publishers.message.set;// That's it ! // When the message is received by the peer they can read it with : listeners.message.get; // { channel:"ch1", message: { oh_hai: "mark" } }
API
init Input :
The init method expects as input an object with the following shape :
- channels: Array (Required)
- subscribeKey: string (Required)
- publishKey: string (Not required)
- PubNub: Pubnub library (Not required)
init Output :
When called init returns an object with the shape :
- listeners:
{ "message | presence | status": ReadOnly<MobxObsevarbleBox> }
- publishers:
{ "message | presence | status": MobxObsevarbleBox }
- destroy:
() => void
Call this method to unsubscribe - pubnub:
Pubnub instance
Instantiated pubnub instance
Pubnub JS Docs
https://www.pubnub.com/docs/web-javascript/pubnub-javascript-sdk