How to Develop a real-time streaming application with PubNub
Your users want live, real-time data pushed to their apps or feeds instantaneously. With PubNub you can achieve that in a few lines of code, regardless of how many users you have or how much data you are publishing.
Let’s say your application is generating a lot of data and you want to distribute that data to your growing user base. For example:
A multiplayer game, sending game state and character movements to all players.
Weather reports for a rapidly changing storm
The latest stock quotes
Aggregating, analyzing, and distributing sensor data
Social media updates for real-time sentiment analysis.
The latest traffic reports nationwide
Whatever your use case, there is a common requirement to stream and publish data in real-time and have that data received by end users as quickly as possible.
If you want to build that functionality for yourself, you would probably adopt a publish-subscribe architecture, since you publish data to multiple subscribers. You would then need to start thinking about how you would scale that solution… What happens if your user base is geographically diverse? What uptime does your cloud provider offer? How are you going to consume this data on the client across mobile, web, and embedded systems?
Luckily, you don’t have to worry about how you will distribute and manage your real-time data streams because PubNub does that all for you. PubNub can consume any amount of real-time data and distribute it to your entire user base within 100-150ms with 99.999% uptime.
Where can you get started?
If you are new to PubNub, our tour will help you to understand the basics, including how to publish and subscribe to data.
We also have a simplified real-time streaming demo that shows PubNub publishing Twitter updates, Wikipedia edits as well as some simulated game and sensor streams.
These are just examples and the published rate of the demos is artificially capped.
Once your data is published to PubNub, the next step is for users to consume it. The demo consumes data using JavaScript but PubNub has many SDKs spanning mobile, server, desktop client, and IoT including all popular languages so you will never need to worry about any of your end users being unsupported.
How can I publish data?
Consider the Twitter updates described earlier, the demo makes use of the Twitter Real-time tweet streamer to receive new tweets which are then published to PubNub and PubNub takes care of delivering the data to all interested endpoints, regardless of scale.
If you are new to PubNub or need a refresher on how to publish data, please check out our Tour.
You would substitute Twitter with your own source of data.
PubNub does not impose hard limits on your publish rate but please refer to the publish rate limit recommendations in our documentation. In summary, if you have a very high throughput application (publishing more than 10-15 messages/sec per channel) you should speak with one of our experts who can help with your application architecture.
How can I receive data?
To understand how to receive data, let's look at another stream example, the Wikipedia stream, which is driven by Wikipedia’s event stream.
Any application who wants to receive the data can subscribe
to it. The code below is all you need to receive data from the demo Wikipedia stream.
For your own app you would need to change the subscribeKey
and channel
name to match how you are publishing your data.
Load the PubNub library, for JavaScript you can do this from the CDN but you can use any PubNub SDK:
For the data recipient, create a PubNub object with the same subscribe key as the data publisher, then subscribe to receive messages. You can copy the code below into a blank web page and see Wikipedia updates sent to the JavaScript console.
PubNub does not impose hard limits on your subscribe rate but you may be limited by other hardware or network factors. Please refer to the rate limit recommendations in our documentation and speak with one of our experts who can help architect very high throughput applications.
Want to implement your own real-time streaming application?
All the source code for the demo front-end is available from github, feel free to use that as the basis of your own streaming application. The front-end uses the PubNub JavaScript SDK but you are not tied to a specific language.
Sign up for a PubNub account and create your own keys. Try publishing your own data in PubNub to test performance and capability. PubNub offers HIPAA, GDPR, and SOC2 compliance along with a robust security model so we can cater to all data. Remember that demo keys are rate-limited to 5 requests per second so please sign-up and generate a real key prior to testing performance.
Want to discuss things further? Talk to a technical sales representative.