Free up to 1MM monthly messages. No credit card required.
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:
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:
Now we’re all set up to grant people access to channels. All we need to do now is call
Let’s break it down.
There is the
channel: we want to grant access to. We use
channel + ',' + channel + '-pnpres', because using a comma lets us grant the same access to two channels at once.
auth_key is the same as earlier. This is who we want to grant access to.
read: true let us subscribe, and
write: true let us publish.
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!
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 PubNub presence, we also need to grant ourselves access to the
channel + '-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.
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
channel_name, and make sure PAM is enabled.
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.
Check out the full demo below:
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 PAM permissions “server side” in a simulated NodeJS environment.
Display real-time notification badges with PubNub and React Native to display important information and bring users back to your...
The concept of Digital Twins has evolved over the last two decades, however, one thing remains the same: the need for real-time...
Learn the ins-and-outs of PubNub message size and get a better idea of how many text characters fit in a single message.