Build Real-time Chat Apps in 10 Lines of Code
Good News! We’ve launched an all new Chat Resource Center.
The SDK or APIs in this tutorial may be out of date. We recommend checking out our new Chat Resource Center, which includes overviews, tutorials, and design patterns for building and deploying mobile and web chat.
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 ( try it on your mobile device too! ):
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:
Build Real-time 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 Real-time 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/10 second. These functions are available free in PubNub’s Real-time 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 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.
- Access Manager allowing fine-grain read/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 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 Presence, you can instantly detect when users enter/leave your app when they disconnect from the network. 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 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.
Check out lots of interesting ways to use presence: Five Ways You Can Use Presence.
Chat History Storage & Playback