Fine grain access control to streams of data by person, device or channel

Access Manager Getting Started Guide

Fine grain access control for streams of data by person, device or channel in a few simple steps with PubNub Access Manager

Access Manager Getting Started Guide

1

Install PubNub

To start publishing and subscribing, we first need to access our PubNub libraries. Include the code hosted on our CDN just before closing your HTML </body> tag.

Now you can instantiate a PubNub object using your own publish and subscribe keys or use our demo keys.

You will also need to enable Access Manager in your PubNub Admin Portal

/* View the Full Documentation. */
/* Instantiate PubNub */
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setPublishKey("Your Publish Key Here");
pnConfiguration.setSubscribeKey("Your Subscribe Key Here");

PubNub pubnub = new PubNub(pnConfiguration);
<!-- View the Full Documentation. -->
<!-- Include the PubNub Library -->
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.[version number].js"></script>

<!-- Instantiate PubNub -->
<script type="text/javascript">

    var pubnubDemo = new PubNub({
        publishKey: 'Your Publish Key Here',
        subscribeKey: 'Your Subscribe Key Here',
        secretKey: 'Your Secret Key Here'
    });

</script>
/* View the Full Documentation. */
/* Instantiate PubNub */
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.PublishKey = "Your Publish Key Here";
pnConfiguration.SubscribeKey = "Your Subscribe Key Here";

Pubnub pubnub = new Pubnub(pnConfiguration);
/***************
For all other languages,
check out the 70+ SDKs that PubNub supports
on our documentation page:
http://www.pubnub.com/docs
****************/

2

GRANT AND REVOKE ACCESS

Permissions now need to be granted to a channel for a specific auth key. In this example, both read and write permissions have been granted for 5 minutes.

/* For grants, read or write permissions need to be set to true */
pubnub.grant()
    .channels(Arrays.asList("Your_Channel_Name"))
    .authKeys(Arrays.asList("Your_Auth_Key"))
    .read(true)
    .write(true)
    .ttl(5)
    .async(new PNCallback<PNAccessManagerGrantResult>() {
        @Override
        public void onResponse(PNAccessManagerGrantResult result, PNStatus status) {

        }
    });
// For grants, read or write permissions need to be set to true
pubnub.grant(
    {
        channels: ['Your_Channel_Name'],
        authKeys: ['Your_Auth_Key'],
        ttl: 5,
        read: true,
        write: true
    },
    function(status) {
        console.log(status);
    }
);
/* For grants, read or write permissions need to be set to true */
pubnub.Grant()
    .Channels(new string[] {
        "Your_Channel_Name"
    })
    .AuthKeys(new string[] {
        "Your_Auth_Key"
    })
    .Read(true)
    .Write(true)
    .TTL(5)
    .Async(new PNAccessManagerGrantResultExt((result, status)=> {

    }));
/***************
For all other languages,
check out the 70+ SDKs that PubNub supports
on our documentation page:
http://www.pubnub.com/docs
****************/

Similarly, when access needs to be revoked, the same channel and auth key need to be specified.

/* For revoke, read and write permissions need to set to false */
pubnub.grant()
    .channels(Arrays.asList("Your_Channel_Name"))
    .authKeys(Arrays.asList("Your_Auth_Key"))
    .read(false)
    .write(false)
    .async(new PNCallback<PNAccessManagerGrantResult>() {
        @Override
        public void onResponse(PNAccessManagerGrantResult result, PNStatus status) {

        }
    });
// For revoke, read and write permissions need to set to false
pubnub.grant(
    {
        channels: ['Your_Channel_Name'],
        authKeys: ['Your_Auth_Key'],
        read: false,
        write: false
    },
    function(status) {
        console.log(status);
    }
);
/* For revoke, read and write permissions need to set to false */
pubnub.Grant()
    .Channels(new string[] {
        "Your_Channel_Name"
    })
    .AuthKeys(new string[] {
        "Your_Auth_Key"
    })
    .Read(false)
    .Write(false)
    .TTL(5)
    .Async(new PNAccessManagerGrantResultExt((result, status) => {

    }));
/***************
For all other languages,
check out the 70+ SDKs that PubNub supports
on our documentation page:
http://www.pubnub.com/docs
****************/

3

Demo

In this demo, grant and revoke access for Alice, Bob, and Jim by using the toggles underneath their avatars. The console immediately updates with the latest calls received over PubNub.

Chat Room Participants
Alice
No Access
Bob
No Access
Jim
No Access
Console

4

You are now a master of Pubnub Access Manager!

With those few steps, you've built your first PubNub application with Access Manager! For even more details on this feature, check out our technical documentation.

If you haven't already, Sign Up for a free PubNub account. Then continue with our other feature guides for Presence and Storage & Playback to make your app more robust.

Get coding right away using our 70+ SDKs in your favorite language, or build on top of one of our demo applications.

logos