Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

rakannimer/pubnub-mobx

Repository files navigation

Pubnub Mobx

CircleCI NPM BundlePhobia

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
import PubNub from "pubnub";
import { init } from "pubnub-mobx";
const { pubnub, listeners, publishers, destroy } = init({
 // Optional, use if you need to publish in addition to listening to data
 publishKey: credentials.publishKey,
 subscribeKey: credentials.subscribeKey,
 channels: ["ch1"],
 // Optional
 PubNub
});

Listen to data change

import { observe } from "mobx";
// ... Code from previous section
observe(listeners.message, ({ newValue, oldValue }) => {
 // Run any logic here on newValue to handle the data
});

Map events to your own data structure

import { computed } from "mobx";
// ... Code from previous section
const latestMessage = computed(() => {
 return { message: listeners.message.get() };
});

Publish a new message

To publish a new message just set it in publishers.message

publishers.message.set({
 channel: "ch1",
 message: {
 oh_hai: "mark"
 }
});
// 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

MobX Docs

https://mobx.js.org

About

Convert Pubnub data streams to mobx observable boxes.

Resources

Stars

Watchers

Forks

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /