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.
Introducing Babel, an open source chat widget and API built with PubNub. Babel allows you to send and receive encrypted, self destructing messages and exchange 1024-bit RSA public keys in a chatroom. In this five part series of blog tutorials, we'll show you how to build a self destructing chat app.
Think Snapchat, but in a chatroom.
We have a live working Babel self destructing chat demo here. Just click the users name you want to send a message to on the left, then write and send your message. You can set the time on each message by changing the number in the box on the right!
You can also take a look at the source code on our Babel Github Repository. Let's get the tutorial started! Or check out parts 2 through 5 below:
- Part Two: Built a key exchange system, enabling you to share public keys between users.
- Part Three: Walked you through how to send TLS encrypted chat messages.
- Part Four: How to send self destructing chat messages
- Part Five: Building a chatroom UI
Building Self Destructing Chat
The Setup
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. Then, import the PubNub JavaScript SDK and to initialize PubNub with your unique keys and SSL enabled.
To start using Babel, we'll have to import the PubNub JavaScript SDK, babel.js
, and cryptico.js
. We can then instantiate a Babel object by calling Babel
with a username string.
Public Key Exchange API
Babel uses 1024-bit RSA encryption to secure the messages it sends. You can view another user's public key with the listUsers()
method, which returns an object with all the currently connected users' usernames and their public keys.
Babel uses a slightly modified form of the Cryptico library to perform RSA encryption. The myKey() method returns your Cryptico RSA key. Checkout the Cryptico README to see all the cool things you can do with your own Cryptico RSA key.
Encrypted, Self Destructing Messages API
To send an encrypted, self destructing message with Babel, use the sendMessage(recipient, message, ttl)
method. recipient
is the username of the user you're sending the message to, message
is the text you want to send, and ttl
is the number of seconds you and the receiver can read the message before it self destructs.
onMessage(callback)
let's you set a callback function that will be called whenever you receive or send a message.
Now if another user sends you a message, messageHandler
will be called and print out the message.
To quit using Babel, use the quit()
method. After calling quit()
other users will no longer be able to retrieve your public key or send messages to you.
To detect when a user calls quit()
, joins, or times out of Babel, use the onPresence(callback)
method. onPresence(callback)
is similar to onMessage(callback)
, except for the fact that no arguments are passed to the callback.
returnMessages()
returns all the messages that you've sent or received, that haven't timed out yet.
… And that's it! With these seven methods you can now use Babel to perform public key exchange and send encrypted, self destructing messages. Over the next couple weeks, we'll be releasing four other blog posts to show you how Babel implements its functionality with PubNub.
Onto Part Two: Building a key exchange system.
Get Started
Sign up for free and use PubNub to power real-time chat