How To Write a Realtime Chat App in 10 Lines of Code

Stephen Blum, PubNub
Jul. 29 2014, 10:00AM EDT

This article was published as a part of ProgrammableWeb's Sponsored Content Program. The opinions expressed here are those of the underwriter do not necessarily reflect the views of ProgrammableWeb or its editorial staff. For more information regarding ProgrammableWeb's Sponsored Content Program, please consult our FAQ.

With PubNub, you can build real-time chat in 10 lines of JavaScript. This is the quickest way to write a real-time chat application on web or mobile.

PubNub’s framework dramatically reduces your coding when building chat based collaboration apps. Tasks like sending and receiving data take only one function call. And your code instantly updates in web and mobiles apps, for rapid testing. The network infrastructure and scaling is taken care of for you so you spend time building your app, not infrastructure.

Chat Input:

Chat Output:

 
 

Copy and paste the code below into your favorite text editor and save as .html.

The basic send “publish” functionality with sending a message for simple chat apps:

The basic receive “subscribe” functionality to receive messages in a simple chat app:

For more information on our JavaScript SDK, check out the GitHub Repository here. Additionally, we’ve created a more advanced chatroom demo, which can be found here. And now you can equip your chat communication source code with high-security options using Self Destructing Chat: Key Exchange and Self Destructing Messages with Public Key Exchange using RSA Asymmetric Cryptography for chat messages.

Build Chat Apps Fast and Cut Development Time

PubNub’s framework dramatically reduces your coding. Tasks like sending and receiving data take only one function call. And your code instantly updates in web and mobiles apps, for rapid testing.

The network infrastructure and scaling is taken care of for you so you spend time building your app, not infrastructure.

Develop Chat Across Platforms

PubNub has the only complete solution for developing across platforms and OS versions. You can write once and build Web, iOS and Android Apps. PubNub will automatically broker your connections from web to phones to tablets to everywhere.

How Chat with PubNub Works

PubNub is a global Data Stream Network that makes it easy to develop and deploy real-time mobile and web apps, using just two functions (Publish and Subscribe) to pass data back and forth in under 1/4 second. These functions are available free in PubNub’s Realtime Software Development Resources, Tutorials & SDKs for over 50 programming languages and frameworks. These simple APIs push messages onto the PubNub Network, which has 14 Points of Presence around the world for low latency and reliable connectivity everywhere. Since PubNub works on just about every conceivable device, it’s now possible to build powerful one-to-one and many-to-many chat applications that work on every device, everywhere in the world.

PubNub is designed to make real-time mobile and web apps easier to build with SDKs to pass data back and forth using JavaScript and other mainstream languages.

PubNub’s Global RPC Network manages XHR cross-domain routing, serialization and parsing so that you don’t have to deal with AJAX. It’s built using JavaScript extended with every optimization under the sun. PubNub creates SDKs with publish (send) and subscribe (receive) functions. Messages published are automatically synced in real-time with clients subscribed. PubNub offers an abstraction layer to make calling remote functions very simple without worrying about ports or sockets. It is a higher-level interface for making network programming easy.

Chat Security

PubNub offers many optional layers of security giving you the easy-to-use tools necessary to deploy an encrypted chat solution that incorporates powerful features like non-repudiation and secure data archival for audit purposes, where needed. PubNub also provides a plug-in replacement for Google GCM Android Cloud Messaging Notifications and Apple APNS Push Notifications, allowing you to “wake-up” a smartphone with a secure message without leveraging unsecured Apple and Google APIs.

PubNub’s secure features for chat include:

  • Built-in AES256 encryption libraries, so chat messages are encrypted at the source. AES keys are not generated or kept by PubNub, so the data is only ever visible by the devices, which contain the AES keys.
  • SSL encryption to ensure the message envelope is encrypted to/from the PubNub Data Stream Network.
  • PubNub Access Manager allowing fine-grain real/write privileges on every chat “channel”, giving you the simultaneous ability to have secured one-to-one chat and unsecured group chat sessions within the same app.
  • Ability to dynamically create random chat “channels”, ensuring the anonymity of users and their locations, even when they are in a private chat together. The user’s IP address and other identifying information is never exposed.

Chat Presence Detection & Participant List using PubNub Presence

A common question is “who’s listening in my chat rooms“? Most real-time apps want to show their end-users who else is there (i.e. subscribing) to a channel. This functionality is a common requirement in chat apps (think about the green icon that appears next to someone’s name), as well as within game chat room lobbies and many other similar apps. Using PubNub Presence, you can instantly detect when users enter/leave your app when they disconnect from the network. PubNub Presence is easy to use: every chat channel contains an optional “presence” channel on which “connect” and “disconnect” events are sent. This provides you an easy way to display icons when a user is in a chatroom, and remove those icons as soon as a user disconnects.

Presence gives you more information that just connect/disconnect events. Presence also gives you a real-time count of the number of people in a chatroom (i.e. “subscribed to a channel”). And, with PubNub Presence, you can pass in a user ID and PubNub will show a list of all chatrooms a user is connected to. This gives you an easy-to-use way to let users “find” their friends in within an application that may contain many chatrooms, games, or collaboration spaces. This presence data is available to end-devices, as well as your servers. To check out other interesting ways to use presence: Five Ways You Can Use PubNub Presence.

Chat History Storage & Playback

When a user enters a chatroom, they often want to see a list of recent chat messages that occurred there. The PubNub Storage & Playback service gives you a single API, PubNub.History(), that will load messages that have previously been typed. The PubNub Storage & Playback system is highly redundant and deployed globally, ensuring that the chat messages are resilient and reliably sync’ed in the cloud, even if there is a disruption in the connection. Learn more about chat history API options starting with JavaScript Storage & Playback for Real-Time Apps.

With 5 years of development on SDKs for every device and environment, it’s now easy to deliver chat on every device imaginable, including iOS, Android, JavaScript, Windows Mobile, Blackberry, and many other resources and SDKs for devices needing realtime software including support for web browsers IE4+, Safari 1+, Opera, Chrome 1+, FireFox 1+ and more.

Sign Up For Free Account

To sign up for a free PubNub account and start playing, GO HERE.

For more information about PubNub’s technology, visit www.pubnub.com.

Stephen Blum Stephen’s vision and strong technical chops drove the initial architecture of PubNub, building a core engine that has successfully scaled to the some of the largest realtime deployments in the world. Stephen has been a successful software entrepreneur his whole life, starting his first company, LTSun, at age 17, and joining Microsoft after its acquisition. Stephen was the chief architect for TrustedOpinion.com, a 50-language social recommendations site that grew to over 1 million users across over 100 countries.

Comments