Real-time Chat Blog

Building a Key Exchange System to Send Encrypted Messages

3 min read Michael Carroll on Jul 23, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

How to build a key exchange system using PubNub, enabling users to send encrypted, self destructing chat messages in real time.

Good News

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 →

Key Exchange System for Sending Encrypted MessagesIn this blog post, we’ll cover how to build a key exchange system with PubNub, and once we do so, we’ll be able to send encrypted messages between users.

Here’s what we’ve covered so far:

This is a five part series on building Babel, an open source chat widget and API built with PubNub. Babel allows you to send and receive self destructing, encrypted messages and exchange 1024-bit RSA public keys in a chatroom.

We have a live working Babel self destructing chat demo here. You can also take a look at the source code on our Babel Github Repository. Now, let’s get the tutorial started!

Building a Key Exchange System

Exchanging public keys is a fundamental component of Babel and developing self destructive messages. Once two users have exchanged public keys, they can then send encrypted messages to one another.

So how does Babel use PubNub to exchange public keys? Let’s walkthrough how you can build your own key exchange system with PubNub.key exchange

Step 1: Import the PubNub JavaScript SDK

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, as well as SSL enabled and with uuid set to a username that other users will be able to identify us with..

Step 2: Public Key Sharing

Our next step is to get a public key to be shared. For the purposes of this blog post, we’ll just use a random string to substitute for our public key. However, a more realistic public key string would be something like the Base64 encoding of an RSA public key.

After we get a public key, we can share it with the PubNub Presence feature. To do this, we first subscribe to a PubNub channel and set our state to an object that contains our username and public key.

Integrating PubNub Presence

PubNub Presence allows users to see who is online and offline, and updates in real time as users log on and off. After subscribing to a channel, other users will be able to see our state with PubNub Presence.

here_now gets a list of unique user-ids currently subscribed to the channel, the total occupancy of the channel, and also the state information of all users subscribed to the channel since we set state to true in our function call.

The data returned by here_now to the callback will look something like this.

Thus, to get the state of all the users subscribed to the channel, you can loop through the uuids array from the the data returned by here_now.

As you can see, exchanging your public keys with PubNub is really easy! However, we’re not done yet. In future blog posts we’ll go over how to send encrypted messages and self-destructing messages through PubNub.

More from PubNub

How to Add a Notification Badge to Icons in React Native
Real-time Chat BlogDec 19, 20226 min read

How to Add a Notification Badge to Icons in React Native

Display real-time notification badges with PubNub and React Native to display important information and bring users back to your...

Michael Carroll

Michael Carroll

Digital Twins and the Future of Real-Time Data
InsightsDec 6, 20224 min read

Digital Twins and the Future of Real-Time Data

The concept of Digital Twins has evolved over the last two decades, however, one thing remains the same: the need for real-time...

Michael Carroll

Michael Carroll

How Many Text Characters Fit in a 32KB PubNub Message?
Real-time Chat BlogNov 24, 20224 min read

How Many Text Characters Fit in a 32KB PubNub Message?

Learn the ins-and-outs of PubNub message size and get a better idea of how many text characters fit in a single message.

Michael Carroll

Michael Carroll