What is Publish vs Subscribe and Real-Time Data Streams?

4 min read Michael Carroll on Jan 12, 2015

We talk a lot about data streams as the core of real-time technology. But what exactly is a data stream? In this blog post, we’ll give you a quick and easy to understand explanation of what a real-time data stream is, and how it fits in the publish/subscribe paradigm.

To start, data is in the form of JSON messages. The stream is a continuous inbound flow of these messages. The messages themselves are encapsulated (individual) and received one at a time in quick succession. The workflow is simple, there is a message publisher and any number of message subscribers. This is commonly known as publish/subscribe or “pubsub”. When a message is published, the subscribers receive it nearly instantaneously.

publish subscribe pubsub design paradigm diagrams

What can be a publisher?

What do publishers look like? What kind of device can be a publisher? The beauty of this, is almost anything can be a publisher, all that is needed is a connection to the Internet.

The recent explosion of Internet of Things, devices with embedded WiFi or Bluetooth Internet connectivity naturally fit with a PubSub paradigm. Large devices like cars, kiosks, digital highway signs, and small things like Arduino’s, Raspberry Pi’s, connected watches, remote controls, and tiny connected sensors can even become publishers.

All the more commonly thought of devices can publish as well, phones, tablets, laptops, browsers, native apps, basically anything that is a connected computer can make use of PubNub. Of course every one of these can be a subscriber as well. We have over 70+ SDKs to make this a breeze on almost any platform out there to easily send and receive messages using PubNub. The SDK’s are so small that embedding them in hardware devices is efficient.

pubsub publish subscribe diagrams

How to think about Data Streams: Use Cases

Stock Quote Streaming Application

The first example that comes to mind when you think of a stream of data is stock prices. The are always changing, and if I want to make an application that displays the current stock prices in real time, I need to receive these price changes as quickly as they happen. That’s where a Publish/Subscribe design comes into play. Stock prices can be broadcast from a publisher to any number of subscribers, and prices are updated in real time.

Taxi Dispatch Application with Geolocation Data

For a second example, let’s think about geolocation. Isn’t a fleet of cabs always on the move? I can have each car in the fleet publish its current location every couple seconds and anyone who subscribes see their updated location on a dynamic map. I can see all those cars moving in real time, or if it’s rush hour, see how many aren’t moving at all. These publishes, happening every couple seconds by each car is creating streams of geolocation data.


A third common example comes in the form of communication. When two or more people are communicating, real-time creates a feeling of connectedness between them. It mimics real life interaction. Real-time chat is like bread and butter using PubNub. So easy to do, and so fast, scalable, and reliable, that nothing else compares.

I remember in the mid 1980’s making international calls to my relatives across the globe. Back then, sound was converted to electrical signals over copper wire based land-lines. Electricity through copper travels far slower than light, and there would be a long delay after I said something before the other side heard it and said something back (in my memory it was about 5-8 seconds). Pretty choppy conversation.

Multiplayer Gaming

What about multiplayer gaming? You can keep everyone’s view of their teammates and enemies in sync in real time. In fact, there are some strong benefits to using PubNub instead of direct P2P connections (where each computer or console is connecting directly with each other).

Those three are some of the more obvious use cases. But there is much more to this than meets the eye. If I can publish any data, and receive it somewhere else so quickly, it opens up a lot of possibilities.

What makes it so fast…err, real-time?

All data transmission to and from a device uses what is called a socket. Unlike an electrical socket, a socket in this case isn’t exactly a physical thing, but there are a lot of parallels. If I plug my phone into an electrical socket, I receive a flow, I am connected to that socket. In computing, a socket allows for sending and receiving data.

What makes PubNub particularly awesome? We have optimized this paradigm so well, that we have a 99.999% SLA for our pubsub latency of 100 milliseconds globally. Yes, five 9’s! What does that mean? It means when I publish a message, it will be delivered to all subscribers, anywhere in the world, within 1

second. Not only that, we can handle enormous volumes of subscribers, millions of them and still get those messages delivered.

If you understand the implications of that impressive feat, you can now see where the real-time comes in. I can create an application that serves up data in real time, and all the subscribers of my data will receive that data in under 100 milliseconds globally.

Getting Started with Publish Subscribe

  1. Sign up for a PubNub account and get your publish/subscribe keys
  2. Head over to the publish/subscribe quick start. You’ll learn how to use the PubNub API and start sending data in 5 steps.
  3. Make your own app in our 100% free sandbox tier!