PubNub Portal

Granting and Revoking Permissions – Private Chat API

0 MIN READ • Sep 9, 2014

Waving Hand

Good News! We've launched an all new Chat Resource Center.

We recommend checking out our new Chat Resource Center, which includes overviews, tutorials, and design patterns for building and deploying mobile and web chat.

Take me to the Chat Resource Center →

Now that we're just about done setting up our private chatrooms with private channels, and adding access control and chat alert popups, we're ready to grant access to channels. This will allow us to grant or revoke access to private channels.


We've now covered both building a multiplayer game lobby with a chatroom and the different ways we can use matchmaking to connect two different users. Here's what we've covered so far:

This blog post is Part Eleven of PubNub Developer Evangelist Ian Jennings‘s series on creating a multiplayer game with JavaScript.


Getting Started with the Private Chat API to Grant/Revoke Access

You’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Once you have, clone the GitHub repository, and enter your unique PubNub keys on the PubNub initialization, for example:

PubNubKeys.html
<!-- Include the PubNub Library -->
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>

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

  var PUBNUB_demo = PUBNUB.init({
    publish_key: 'Unique Publish Key Here',
    subscribe_key: 'Unique Subscribe Key Here'
  });

</script>

Now we're all set up to grant people access to channels. All we need to do now is callpubnub.grant().

1.js
pubnub.grant({
  channel: channel + ',' + channel + '-pnpres',
  auth_key: me.uuid,
  read: true,
  write: true,
  ttl: 0,
  callback: function() {
});

Let's break it down.

There is the channel: we want to grant access to. We usechannel + ',' + channel + '-pnpres', because using a comma lets us grant the same access to two channels at once.

The auth_key is the same as earlier. This is who we want to grant access to.

The properties read: true let us subscribe, and write: true let us publish.

The ttl: setting is a time for the grant to expire and it is set to 0 so it lasts forever.

There is also callback but if you don't know what thats for by now you should go back to tutorial #1!

Grant Before Subscribe

We're going to wrap our pubnub.subscribe call in a grant call so we have sufficient permissions before we try to connect.

Because we're using Presence, we also need to grant ourselves access to thechannel + '-pnpres' channel.

We'll grant ourselves permissions in channel, wait for the callback, grant ourselves permission in chanel + '-pnpres', wait for the callback, and then finally subscribe.

2.js
pubnub.grant({
    channel: channel,
    auth_key: me.uuid,
    read: true,
    write: true,
    ttl: 0,
    callback: function() {

      pubnub.grant({
        channel: channel + '-pnpres',
        auth_key: me.uuid,
        read: true,
        write: true,
        ttl: 0,
        callback: function() {

          pubnub.subscribe({
            channel: channel,
            state: me.state,
            message: function(data) {

              // ...

            }

          });

        }

      });

  }

});

Now when we run the code, we should connect without errors. This will grant everybody access to the channel and presence channel when they load the page.

If you experience errors, check your auth_key, channel_name, and make sure Access Manager is enabled.

Make Private Channels Private

Now we're going to do the same exact thing for the private user to user channel. When a user opens the modal, they'll give themselves access to the private chat channel. No other users will have access to that channel besides the two users who start chatting.

3.js
var init = function() {

  pubnub.grant({
    channel: private_channel,
    auth_key: me.uuid,
    read: true,
    write: true,
    ttl: 0,
    callback: function() {

      $('body').append($tpl);

      pubnub.subscribe({
        channel: private_channel,
        message: function(data) {   
          // ...
        }
      });
    }
  });

};

Private Chat API Demo

Check out the full chat API demo below:

See the Pen Memewarz – Private Chat #3 by Ian Jennings (@ianjennings) on CodePen.0

Real Security

private chat api

As of now, users can grant access to themselves (and that's not secure now, is it?) In our next tutorial we'll change that, and move the Access Manager permissions “server side” in a simulated NodeJS environment.

Get Started
Sign up for free and use PubNub to power
real-time private chat!

Sep 18, 2014

Emulating a Secure API Server in Chrome Web Browser

How to run a secure API server in a web browser. In this case, we'll write the server in JavaScript on the front-end.
Michael Carroll
Michael Carroll
Chat
Oct 23, 2013

Secure Chat Using Access Manager

How to build a secure chat application using Access Manager for fine grain, serverless control over chat users.
Michael Carroll
Michael Carroll
Mar 14, 2012

HTML5 Mobile Chat Easily with jQuery | PubNub

This source code and tutorial walks you through how to build a simple HTML5 mobile chat application with jQuery.
Michael Carroll
Michael Carroll