Building with real-time messaging: benefits, basics, and first steps
Real-time Messaging (or RTM) refers to the distribution and delivery of messages that are designed to be consumed or otherwise used in real time (i.e. as events occur and no later). This could be a chat message, a stock-ticker update, or even an IoT sensor reading.
This technology underpins a new generation of dynamic applications by powering features like instant communication with others, persistent data monitoring, and control over connected devices.
This guide will explore this technology in depth, taking a look at:
Where and when real-time communication is useful.
The unique opportunities for real-time communication in chat apps.
How real-time messaging works.
How PubNub’s API and chat platform make it easy to bring real-time functionality into any app.
By the end of this guide, you’ll understand why real-time messaging is the best way to bring responsive, dynamic connectivity to your users, and you’ll have a clear path towards implementing it in your own app with PubNub.
When is real-time functionality useful?
Real-time messaging systems are perfectly suited for building a wide array of applications. We’ll dig into how they work in the next section, but for now, it’s enough to know that they play a key role by rapidly delivering any data between any type or number of devices.
Generally speaking, this lets real-time messaging systems play a key role in two broad categories of apps:
Those that connect people with one another, like social media apps or work collaboration tools.
Those that connect people to machines, like logistics monitoring platforms and IoT control suites.
Connecting people through remote interactions and live events
For apps that connect people with one another, real-time messaging enables a broad array of interactions that help digital experiences of all kinds feel as immediate and natural as their in-person equivalents.
Real-time messaging powers features like arenas for multiplayer games, audience-wide polls for virtual live events, and collaborative whiteboards for elearning platforms. In all these cases, real-time messaging provides instantaneous and scalable communications between all users.
One of the strongest use-cases for real-time messaging is chat. Chat apps inherently set out to connect the people that use them, and the immediacy of a chat experience — ensured by real-time communication— is a crucial part of helping remote, text-based conversations feel like genuine interpersonal interactions.
As with real-time messaging itself, chat holds powerful benefits for many kinds of apps. Mobile game studio Pocket Gems, for example, found that in-the-moment camaraderie fuels player relationships and strengthens in-game communities.
For its virtual live events, LiveLike’s audience chat lets fans join in the energy of the crowd for pivotal moments like celebrating a touchdown.
And, in sensitive use cases like telemedicine, the immediacy and authenticity of chat drives positive care outcomes by creating a space for patients and doctors to share honest communication.
No matter the end-use, messaging apps at large rely on real-time communication to send and receive chat messages instantaneously. Beyond this essential functionality, RTM is also the basis on which developers build value-add features like user presence, typing indicators, and push notifications.
In general, real-time messaging connects people because it lets everybody see and interact with the same information in the same moment. This brings the immediacy and genuine energy of in-person experiences directly into remote interactions.
Bridging people and IoT devices
Importantly, it's not only humans that benefit from real-time messaging. It is also essential for autonomous, distributed, or smart devices that must work in tandem to achieve a common goal, and that must report their status out in a way that users can understand and act upon. IoT device control, industrial process monitoring, and even medical monitoring services like PubNub customer Hearo make use of real-time messaging to:
Drive processes that depend on intrinsically unpredictable events, like adjusting sprinkler water usage based on fluctuating outdoor temperatures.
Provide a synchronous overview of different parts of a larger environment, like a smart-home app that grants access to lighting, temperature, and locks all in one place.
Ensure awareness of the present state of a system, such as a logistics dashboard to monitor the health of vehicle fleets at a glance.
When smart devices and distributed systems leverage real-time messaging, they can provide measurements that are accurate up to the second, and give users the power to respond to that data with instant controls.
How does real-time messaging work?
From the wide array of use-cases we’ve covered, it’s clear that real-time messaging is an incredibly versatile technology. This capability arises from two major factors:
Data is sent using flexible and resilient messaging patterns like publish/subscribe.
Messages themselves are packaged using common formats like JSON.
Let’s dig into each of these factors, and explore how a service like PubNub leverages both to simplify real-time app development.
Rapid and resilient messaging with publish/subscribe
Real-time messaging on the web typically involves a system in which data is streamed or pushed to users, applications, or devices with a low-latency, real-time delivery mechanism. PubNub, for example, implements a publish/subscribe (pub/sub) paradigm.
In pub/sub systems, data is published over a channel in the form of messages. Any device subscribed to that same channel will rapidly receive those messages as they’re published.
Package data for easy consumption with JSON
Structuring and using real-time messages
How a message is actually used depends on the needs of the application. A chat app, for example, needs to display the messages that users send to one another, along with additional information like the name of each sender. Following this example, the original JSON payload could contain:
The name of the user that sent the chat
The chat message itself.
The time at which the message was sent.
Based on these contents and the channel on which the message was sent, a subscribed chat app would then be able to:
Populate the message body and sender name.
Find and display the sender’s profile picture based on the sender name.
Show the time at which the message was sent.
Display the completed chat message in the appropriate chat room.
Using PubNub, developers can fully customize the structure of channels and messages. This affords a huge amount of flexibility, and means that teams using PubNub can fully customize their implementation of real-time messaging to meet their precise development needs.
Real-time messaging vs webhooks: which works best?
The concept of real-time messaging can also be compared with something as simple as a webhook, which lets you push updates out from an app to external URLs as they occur. This can be used to invoke a 3rd party service, update a database, or trigger business logic on a server in response to user activity in an app.
While the functionality enabled by webhooks has some things in common with real-time messaging, it’s best to think about webhooks as specialized tools to interact with specific endpoints rather than as a core messaging service that can connect many participants.
This is because implementing webhooks requires linking the app that’s triggering behavior to a specific receiving URL, where that behavior will actually occur. This single-endpoint limitation makes them poorly suited for things like chat and collaboration. In these use-cases, messages need to be sent between a large and unpredictable number of recipients.
By contrast, real-time messaging, like that available through PubNub, can reliably ferry large volumes of messages between an unlimited number of participants. This is because RTM based on pub/sub fundamentally decouples the sending and receiving of messages. In other words, a publisher can send messages without regard to the type or number of subscribers, and vice-versa.
This pattern lends itself to flexible, scalable communications, letting PubNub easily support the majority of use-cases where it’s inefficient (or impossible) to specify message recipients ahead of time.
How can I implement real-time messaging in my application?
When it comes to bringing real-time functionality into your app, you have a wide array of tools, technologies, and techniques to choose from. Whether you develop from the ground up or build on top of an existing service, the route you choose will depend largely on your app’s needs, your timeline, and your total development resources.
Option 1: Leverage web messaging protocols to build from scratch
On a basic level, real-time messaging patterns are enabled by a variety of web protocols that let devices freely stream packets of data to one another, without waiting for either party to explicitly request that information.
XMPP, WebSockets, and HTTP Long Polling are all good examples of real-time messaging protocols that support high customizability and extensibility, and that can serve as the foundation for a real-time application.
While open-source frameworks exist in many programming languages to support and use these protocols, actually leveraging them as part of your technology stack means handling a much longer list of challenges, including:
Provisioning and hosting servers
Ensuring low latency for all users
Managing user connectivity, especially for mobile users that may be traversing networks
Designing the data structures for users, messages, and channels to work together flawlessly
Running and spinning up VMs as your service scales
Replication and POPs
Maintaining all of this into perpetuity
A dedicated engineering team can certainly build from the ground up. But, in reality, doing so is both an up-front and ongoing investment in time, effort, and resources. This means diverting from core development just to establish basic messaging. The truth is, most teams looking to add real-time features need to do so without first needing to design, build, and maintain their own specialized infrastructure.
Option 2: Accelerate development with a real-time API
A far better option for most teams will be to use a hosted, API-based service for real-time messaging that has already solved the major networking challenges, drastically simplifying the development and deployment of new features.
An API-based service like PubNub brings all the power and flexibility of real-time messaging to your team with code-based tools. These let you build real-time functionality without worrying about infrastructure.
Hosted services like PubNub take care of major considerations like speed, scale, and reliability, as well as important but tricky details like connection management, data replication, and traversing complex network topologies. All of this lets your team focus less on simply keeping users connected, and more on the features they want to create.
Choosing a real-time API
Any team looking to integrate real-time communication into their app via an API has many services to choose from. While each may appear to cover the same fundamental needs at a glance, evaluating a service in-depth can be a challenge. As you compare solutions, ask yourself questions like:
Is the API easy to implement?
Will this API let us easily add new features?
Does this API work seamlessly with the tools and technologies I already have?
Can this service deliver a reliably fast experience for my users?
Can this service support my traffic as I scale?
PubNub’s real-time communication platform offers easy-to-understand methods that let teams of all sizes leverage our globally distributed, scalable, low-latency network. Additionally, the flexible design of our APIs mean that any real-time feature can be built on PubNub, and our vast catalog of third-party integrations lets your team connect to the services they already love to use.
Consider the availability of SDKs
Additionally, consider the details of implementation. It’s essential that any API you choose works naturally within the technology stack your team has chosen. Here, SDKs come into play.
SDKs (software development kits) package all the power of a hosted real-time service into a set of intuitive methods for specific programming languages, making it easy for developers to build new features using the tools most familiar to them.
To empower dev teams and remove blockers, an API should offer SDKs for a broad range of platforms and languages.
For mobile apps, an API should offer:
C# Unity for mobile games.
In the ideal case, a real-time API simply never makes you choose. PubNub offers the SDK for your platform, whether you’re developing for web, mobile, desktop, or IoT. This means that PubNub will work anywhere, using the most modern technologies available.
Beyond APIs, chat platforms meet the real needs of app development
We mentioned earlier that chat apps are a common use case for real-time messaging, and it’s worth considering the challenges unique to developing these apps. Real-time and chat go together because the immediate exchange of messages is at the heart of any chat experience. But, instant messaging is only the first piece of a chat experience that will truly speak to the needs of users.
Thanks to the example of Slack, WhatsApp, and chat across the largest social media platforms, users now expect snappy and responsive messaging as a bare minimum. Realistically, they also expect to find polished, quality-of-life features that make a chat experience feel dynamic and responsive.
Crucially, if you are developing any kind of real-time chat, these features are an important part of engaging users and keeping them happy in your app.
To help your team deliver chat apps that can support those authentic interactions, a real-time messaging API should come as part of a mature and developed chat messaging platform, like PubNub. A messaging platform supports chat development by providing out-of-the-box features for:
1:1 and Group chat, for all types of conversations
User presence, to see who’s online
Chat message history, to catch up on past or missed messages
The option to integrate voice and video chat, for genuine remote interactions
Sharing GIFs in-line
Message reactions that keep conversations lively
Custom or unicode emoji support
In-app alerts for mentions and new messages
Typing indicators to create an instantaneous back-and-forth
Read receipts and timestamps so users know when others have seen their messages
Push notifications, so important updates don’t get overlooked
Chat platforms that offer this feature set, like PubNub Chat, make it easy to build a chat experience for your users that brings the best of consumer chat experiences right into your app.
To recap, real-time messaging describes an array of methods that allow users and devices to send, receive, and consume information the moment events occur, and no later. It is common for modern real-time messaging systems to implement messaging patterns like pub/sub, and to structure messages with open data formats like JSON. The combined flexibility of these two approaches enables a vast array of use-cases for an uncapped number of connected devices.
Across industries, from healthcare to multiplayer games, real-time messaging powers digital experiences that have the immediacy, energy, and authenticity of in-person interactions.
Teams looking to build real-time experiences are best served by an API-based service, which will let them implement custom features directly through code. That said, those that are specifically interested in building chat apps, or chat within a broader experience, should look for real-time APIs that are part of mature and reliable chat platforms.
If you’re ready to develop real-time features, get in touch with our team. We’ll get you up and running, so you can start building today.