Access Manager Quickstart

Access Manager Quickstart

1

Install PubNub

To start publishing and subscribing, we first need to access PubNub libraries. Include the libraries from CDN or from popular package managers.

View Full JavaScript SDK Documentation
<!-- Include the PubNub Library -->
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.27.3.js"></script>
View Full Android SDK Documentation
// Using Maven
<dependency>
  <groupId>com.pubnub</groupId>
  <artifactId>pubnub-gson</artifactId>
  <version>4.29.2</version>
</dependency>
View Full .NET SDK Documentation
// Using Nuget
https://www.nuget.org/packages/Pubnub/4.5.0.0
/***************
For the 70+ SDKs that PubNub supports go to:
http://www.pubnub.com/docs
****************/

2

Instantiate PubNub

Now you can instantiate a PubNub object using your own publish and subscribe keys.

<!-- Instantiate PubNub -->
<script type="text/javascript">
    var pubnub = new PubNub({
        publishKey: "myPublishKey",
        subscribeKey: "mySubscribeKey",
        ssl: true
    });
</script>
/* Instantiate PubNub */
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setPublishKey("myPublishKey");
pnConfiguration.setSubscribeKey("mySubscribeKey");

PubNub pubnub = new PubNub(pnConfiguration);
/* Instantiate PubNub */
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.PublishKey = "myPublishKey";
pnConfiguration.SubscribeKey = "mySubscribeKey";

Pubnub pubnub = new Pubnub(pnConfiguration);
/***************
For the 70+ SDKs that PubNub supports go to:
http://www.pubnub.com/docs
****************/

3

Enable Access Manager

Enable Access Manager on your keys in the Admin Console to use this feature.

4

Grant Access

Permissions 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: ['yourChannelName],
        authKeys: ['yourAuthKey'],
        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(Arrays.asList("yourChannelName"))
    .authKeys(Arrays.asList("yourAuthKey"))
    .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(new string[] {
        "yourChannelName"
    })
    .AuthKeys(new string[] {
        "yourAuthKey"
    })
    .Read(true)
    .Write(true)
    .TTL(5)
    .Async(new PNAccessManagerGrantResultExt((result, status)=> {

    }));
/***************
For the 70+ SDKs that PubNub supports go to:
http://www.pubnub.com/docs
****************/

5

Revoke Access

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: ['yourChannelName'],
        authKeys: ['yourAuthKey'],
        read: false,
        write: false
    },
    function(status) {
        console.log(status);
    }
);
/* For revoke, read and write permissions need to set to false */
pubnub.grant()
    .channels(Arrays.asList("yourChannelName"))
    .authKeys(Arrays.asList("yourAuthKey"))
    .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(new string[] {
        "yourChannelName"
    })
    .AuthKeys(new string[] {
        "yourAuthKey"
    })
    .Read(false)
    .Write(false)
    .TTL(5)
    .Async(new PNAccessManagerGrantResultExt((result, status) => {

    }));
/***************
For the 70+ SDKs that PubNub supports go to:
http://www.pubnub.com/docs
****************/

Try PubNub Today

Try Our APIs
  • 1M Transactions
  • 1GB Data Persistence
  • All major Features