Real-time Chat Blog

How to Send a Self-Destructing Message

3 min readPubNub Staff on Jul 30, 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.

This tutorial walks you through how to send a TLS encrypted, self-destructing message in a chatroom that will destruct after a customizable, predetermined amount of time


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 →


In this blog post, we’ll give you a tutorial on how to send a self-destructing message. Once messages are sent in the chat application, they automatically delete themselves after a predetermined amount of time.

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 chat messages and exchange 1024-bit RSA public keys in a chatroom.

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

How to Send a Self-Destructing Message self destructing messages

Step 1: Importing the PubNub JavaScript SDK Provides Long Lived Persistent TCP Connections

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.

Step 2: Finding Users Using PubNub Presence

The first step in sending self destructing messages is to find another user to chat with. As shown in previous blog posts, this can be done quite easily with PubNub Presence. Presence allows you to detect whether users are online or offline in real time.

We subscribe to a PubNub channel and also declare two functions, onMessage and onPresence, that we’ll initialize later when we need to.

onPresence will be called whenever someone joins or leaves this PubNub channel. Thus, we’ll initialize it so that it calls here_now(), which returns a list of all currently connected users. Then we’ll store that list as the variable users.

Step 3: Sending a Self-Destructing Message

Once we’ve found another user on our PubNub channel, we can send him a message by publishing a message onto the PubNub channel. The messages will be objects with recipient, sender, message, and ttl properties. The recipient property allows our recipient to know that this message is intended for him, and the sender property let’s him know that we were the ones who sent the message. The message property is the actual text of the message, and ttl is the number of seconds before the message self destructs.

Step 4: Self Destruction of the Messages

Now that we can send messages, we also want to make these messages self destruct. We can achieve this with the setInterval method. setInterval calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.

Thus, we will initialize onMessage so that it stores all the messages we receive, uses the setInterval method to update the TTL values of each message every second, and eventually deletes each message once their TTLs have expired.

Sending a Self-Destructing Message Demo

Below is a JSFiddle that has implemented all the code in this blog post, along with some extra UI goodies. You can also check out the demo and all the code here. We’re not just updating a property field in a JavaScript object, but also the DOM. When the TTLs reach zero, we get explosions (and the message self destructs!

Stay tuned for the final part of this blog series, were I’ll show you how to implement these pretty effects and build a chatroom UI.

More from PubNub

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff

Another Step Towards Data Security: ISO-27001 Implementation
BuildMay 2, 20221 min read

Another Step Towards Data Security: ISO-27001 Implementation

Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.

PubNub Staff

PubNub Staff