Build a chat app in 20 lines of code

A simple chat app is the gateway to powerful and robust realtime interactivity for web, mobile, and IoT apps.

Dec 21, 2016 · 1 min read

Realtime data streaming has become a fundamental function of today’s apps. Users expect their data in realtime, whether it be a live-updating map, current stock prices, or in this case, a simple chat message. At PubNub, that’s what we power, using our publish/subscribe messaging API, and powered by our data stream network.

To show the functionality off, we created as simple a demo as possible, a chat app in 10 lines of JavaScript.

Check out the live chat demo here (open up two browsers and send messages between them!).

With that, check out the code below.

Sending and receiving messages through PubNub take a single function call.

The basic send functionality happens through a publish()call.

To receive sent messages on a specific channel, make a subscribe() call.

Network infrastructure is already built out for you with PubNub, so feel free to check that off your to-do list.


Written by


APIs for building secure realtime apps, with a global programmable network to power them.