In our previous tutorial on private chat, we left off letting users grant themselves access to protected channels. However, obviously this isn’t very secure if every client has access to the secret key. It’s not private chat if anyone can grant access to themselves. Let’s change that.
Our “server” will be created in a second file that handles permissions and acts as an administrator, and will also handle our game logic or “truth.”
Wait what? Why would you ever want to do this?
Instead of building out cloud servers, you can run a secure API server on your own machine, all in a web browser. You can open up your laptop, and essentially your laptop becomes the server. No backend required.
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.
We’ll start by creating a new html file, which should look familiar.
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:
Here we’ve created another chat window which has no input. It’s going to server as a convenient log of activity happening in our application.
We’ll then add some basic CSS.
Let’s start coding our server. The app will start exactly the same as in the past. Copy the same
secret_key from the other examples.
Great! Now we’re going to add a global permissions grant to everyone on our server.
In the last Private Chat tutorial, we created a new
auth_key on every page load, and then granted that auth_key access. In this example, we omit the
auth_key which will grant everyone permissions.
We grant permissions to two channels at a time by using a comma separated list.
Then we subscribe to the channel. When messages come through, we output them to the server output box. We use
JSON.stringify to format the object as JSON.
Now when we talk in “Lobby Chat” we should see the message output in the “Server Output.”
Remember how our client gave itself permission to private chat channels on request? Now we move that logic to the server.
new-private-chat message comes through, we grant the
uuid who sent the request permission and the
target using a comma separated list just like we did with multiple channels before.
We’ll also output a special alert into the server output that shows we granted a user access.
The full code looks like this:
There’s one more part. Sure we can see the output on the server, but the client still has access to the
secret_key and is granting permissions!
Back on the “client code” (the one with “Lobby Chat”) we remove the secret_key from
Now that the
secret_key has been removed we should remove our grants as well. There are two when the app starts:
And there is one when a
new PrivateChat is made.
Remove all these grants, and we’re done!
See the Pen Memewarz – Server Side [server] by Ian Jennings (@ianjennings) on CodePen.0
See the Pen Memewarz – Server Side [client] by Ian Jennings (@ianjennings) on CodePen.0
There are common underlying technologies for a dating app, and in this post, we’ll talk about the major technologies and designs...