- Realtime technologies
- Guide to PubNub: What it is, how it works and when to use it
Guide to PubNub: What it is, how it works and when to use it
When building realtime features, one of the first things you should consider is how much you want to build yourself, and which technology you plan to use. If you are time or resource-constrained, going with a PaaS provider could be a good fit, and one of the options available is PubNub. This article explains what it is, how it works, its use cases and limitations, and what alternatives there are — everything you need to evaluate PubNub as an option for your realtime needs.
What is PubNub?
PubNub is a platform for building realtime applications, which promises low latency and high reliability and scalability via a globally-distributed set of data centers. It is compatible with various platforms across web, desktop, mobile, and IoT, with various SDKs for rapid development of clients and servers.
PubNub apps are based on a pub/sub architecture, providing communication between server and subscribed clients, and facilities for handling authentication, detecting user presence, sending push notifications, reacting to events, and more. You can develop a PubNub app for free, but will start to incur monthly charges as you scale.
PubNub Key features
Message format: Text, binary (provided it is Base64-encoded), plus PubNub provides file-sharing capabilities
Transport: primarily HTTP long polling, with MQTT support available for constrained bandwidth environments
Global data centers: 15
Authentication provided: Yes
Push notifications available: Yes
Message persistence: Yes, storage API available
User presence: Yes
Flexible messaging patterns: One-to-many, many-to-many, many-to-one. This is possible due to the use of pub/sub channels.
Serverless functions: Yes
How does PubNub work?
Creating a PubNub app usually begins with a visit to the admin portal to configure it.
A PubNub client works by subscribing to the PubNub service using a subscribe key to receive messages and a user ID to identify the client on the platform. Optionally, a publish key is provided to allow the client to send messages. These credentials are passed in during the initialization of the PubNub service.
Once initialized, a device can start to send and receive messages to any channel it is subscribed to. Messages are sent via HTTP long polling, with the most common formats being JSON, plain text, and URL-encoded objects. Once messages are successfully sent, PubNub sends a response containing a timestamp, which is useful when you want to work with historical messages.
An image that shows how PubNub works. Once initialized a device can start to send and receive messages to any channel it is subscribed to via HTTP long polling.
To receive messages, your client needs to be subscribed to the channels the messages are sent on. Your client can respond to messages being received by setting up an event listener.
In addition to events related to messages specific to your app (for example, chat messages in the case of a chat app), PubNub also supports the distribution of other events, such as:
Message metadata matching a pattern you define
User presence events such as users subscribing and unsubscribing to channels
Users changing their personal information
PubNub also has an access manager that uses a system of time-limited unique user ID tokens (UUIDs) to control access to channels, which include granular permissions (e.g. read and/or write access to specific channels). Tokens are requested from PubNub via a grant request and must be included in the initialization code.
For more details including code examples, see the PubNub First Steps documentation.
What is PubNub used for?
PubNub is suited to a range of use cases, including:
E-Learning: Interactive classrooms are a use case that benefits from a service like PubNub. Security is obviously a key concern for schools, colleges, and universities. In addition, realtime data is essential for building fluid, responsive, interactive classrooms, with information updates provided to multiple clients, chat facilities to ask and answer questions, and private channels to provide individual support as required.
Entertainment: In the modern world, entertainment has largely gone online, and customers expect their experiences of concerts, dating, sporting events, and socializing to be just as interactive and “real” as actually being there. This is where a low-latency realtime app platform is useful, combining public and private chatrooms with scores and other statistics that can be updated in realtime, live commentary, polls, location data, and streamed video and audio
Location-aware apps: Realtime messaging is ideal for sending location and other data from and to multiple clients to provide experiences such as navigation, fleet tracking, and taxi and delivery services, with realtime performance updates.
Gaming: Realtime is ideal for gaming use cases, sending realtime game data to update state across multiple clients, building in-app chat for players to find out when their friends are online and communicate with them, providing players with notifications of key game events, and building leaderboards that update in realtime.
Telemedicine: Realtime platforms can be used for building secure, privacy regulation-compliant healthcare portals, with realtime chat functionality for consultations, appointment notifications, and more.
E-commerce/marketplaces: For superior customer engagement and buyer/seller communication, many e-commerce and other trading platforms (such as stock and auction apps) choose to make use of realtime services. Realtime messaging is vital for realtime price updates, prompt communication to enquire about products, postage, and other details, making and monitoring bids, etc.
What are the limitations of PubNub?
All commercial platforms have their pros and cons; you have already looked at what PubNub has to offer in terms of functionality and use cases. Now it is time to consider its limitations.
QoS/delivery order/idempotency: PubNub does not guarantee message delivery, ordering by default, or message idempotency — like other alternatives such as Ably. It is possible to retrieve messages from the message history, and message are given timestamps that could be used to order them, but a bit more implementation work is required in this regard.
Performance: PubNub transports messages via HTTP long polling, which is not as performant as WebSockets or other transport mechanisms. Bear this in mind as you think about data volume and scalability. PubNub advertises sub-250ms worldwide latencies. It’s worth noting that latencies close to 250ms might not be “realtime enough” for certain use cases such as fast-paced multiplayer action games. This can cause a laggy feeling, which can negatively impact user experience.
Lack of delta message compression: Related to the general point about performance above, PubNub does not support delta message compression, meaning that each time a message stream is transmitted, the entire payload is sent rather than just the changes.
Pricing transparency: PubNub's pricing follows a usage-based model. However, pricing is based on over 60 different transaction types, making it difficult to predict ongoing costs.
Lack of JWT support: PubNub is not able to use JWTs, an industry standard for authentication.
Continuity and connection state recovery: PubNub has issues with message continuity. The default PubNub message queue size is 100 messages. If a user is disconnected, and over 100 messages are published while they are disconnected, some messages will never reach the client upon reconnection.
What are the alternatives to PubNub?
There are several other realtime messaging platforms available. Notable alternatives to PubNub are:
Pusher: See Pusher vs PubNub for a comparison between the two.
Ably: For a detailed comparison between Ably and PubNub, see Ably vs PubNub.
Firebase: See Scaling the Firebase Realtime Database beyond 200k users for more information.
SignalR: Learn about the challenges of using SignalR at scale.
You could also consider building your own solution using a library such as Socket.IO. However, you need to be aware that building your own robust realtime messaging infrastructure is a time-consuming and costly process. The report by Ably on The State of Serverless WebSocket Infrastructure outlines that, of the companies surveyed, over 2/3rds of their self-build projects lasted more than three months and 93% needed more than 4 engineers.
Q: Is PubNub free?
PubNub is free when developing apps with up to 200 monthly active users; costs start at $49 / month as your app scales beyond that.
Q: Can PubNub be used with AWS?
PubNub provides several integrations to allow you to integrate functionality from various Amazon Services, as well as other third-party providers. See PubNub Integrations for more details.
Q: Can PubNub stream data to power BI?
PubNub provides a Power BI integration to allow you to stream PubNub data to it, in order to create realtime dashboards. See Real-time in no time with Power BI for more details.
PubNub Alternatives and Competitors
Learn about similar solutions to PubNub that you can use to build realtime features like chat, asset tracking, live dashboards, and mobile push notifications.
PubNub vs. Socket.IO: Learn how they are different and compare their features
PubNub and Socket.IO allow developers to power realtime messaging in web apps for use cases like live chat, multiplayer collaboration, and data broadcast.
Pusher vs PubNub vs Firebase: pros, cons and key differences
This article compares three possible platforms for building a realtime application — Pusher, PubNub, and Google Firebase - to help you make an informed choice.