On this page

PubNub Provider

The PubNubProvider makes a PubNub client instance available to a React component tree.

Required arguments

The PubNubProvider component takes a single client argument, which is the required pubnub instance. This is used by all components that require PubNub functionality.

Example

Instantiate a PubNubProvider as follows:

1import PubNub from 'pubnub';
2import { PubNubProvider } from 'pubnub-react';
3
4const pubNubConfig = require('./pubnub.config.json');
5const pubnub = new PubNub(pubNubConfig.keySet);
6
7const App = () => {
8 return (
9 <PubNubProvider client={pubnub}>
10 <MyRootComponent />
11 </PubNubProvider>
12 );
13};
14
15export default App;

This example assumes that your publish and subscribe keys are contained in a file called pubnub.config.json, similar to the following:

{
"keySet":
{
"publishKey": "myPublishKey",
"subscribeKey": "mySubscribeKey"
}
}

You can also configure the PubNub client directly within the code:

1import PubNub from 'pubnub';
2import { PubNubProvider } from 'pubnub-react';
3
4const pubnub = new PubNub({
5 publishKey: 'myPublishKey',
6 subscribeKey: 'mySubscribeKey',
7 uuid: 'myUniqueUUID'
8 });
9
10 function App() {
11 return (
12 <PubNubProvider client={pubnub}>
13 <MyRootComponent />
14 </PubNubProvider>
15 );
show all 16 lines
Last updated on