WebSockets and Long Polling in JavaScript, Ruby and Python
Whilst this post still contains useful information, we have a newer support article which explains why PubNub should be considered protocol agnostic.
Similarly, the code samples contained within this post target an old version of the PubNub SDK, please refer to the Getting started guides for JavaScript, Ruby and Python for more recent information.
The PubNub Data Stream Network is a very powerful, highly scaled, reliable service for sending and receiving messages across the globe at extraordinary speed and volume. It enables a new class of applications of many varieties that can be real-time and responsive on just about any platform, iOS, Android, browsers, laptops, desktops, servers, Arduinos, Raspberry Pi, sensors, light bulbs, you name it. The only thing required is for the device to have a connection to the Internet and be supported by one of our 70+ SDK’s.
What is a typical Message over HTTP Long Polling or WebSockets
JavaScript Serialized Object Notation, or JSON, has quickly become the new standard in transferring information between applications, browsers and machines. The reason it supplanted XML was because it’s easy to read and more compact — conserving bandwidth and developer friendly. At PubNub we have created a global network that allows you to send and receive JSON messages at impressive speed and enormous scale using
We use JSON when we want to take information and transfer it across the Internet to another application that will use it. This information can be of infinite variety. In Internet of Things category of devices, these messages could be sensor data like temperature, water pressure, or whether a light bulb is on or off. In applications like chat, it can be the text that two or more people are sending to each other.
How is Data Organized on WebSocket and Long Polling Streams?
We organize these streams of messages into Channels. Channels are basically just a context for the messages, a way to organize them. Much like television (or radio), a channel is abstract, it means that anyone watching (or listening) will receive that information.
For example, if we have a channel called AAPL for the current price of AAPL stock, it would contain an (endless) stream of JSON messages with the time, price, change, volume, etc.
These messages are Published to the AAPL channel, and that means an application somewhere is taking the current price from it’s source of information and Publishing JSON messages to the AAPL channel.
Now you might want to display those messages right? Maybe on an iOS or Android device, or maybe in a browser, or maybe in a kiosk, a digital billboard, or maybe all of them simultaneously! In all these cases, it’s actually the same easy process. Each of these devices wants to receive this stream of JSON messages so then need to tune in or Subscribeto the AAPL channel. There is no limit to the number of subscribers or devices, and this is what attracts people to PubNub, it’s sheer scale at your disposal.
WebSockets Diagram
HTTP Long Polling Diagram
Publish Examples in JavaScript, Ruby and Python
Let’s look at examples of how to Publish JSON messages in a few of our SDK’s. In every SDK it follows the same basic workflow:
- Establish a connection to PubNub with your own Application Keys (AppKeys)
- Compose and Publish a JSON Message to a specified channel
In these examples I am doing Publish only, otherwise I would also provide the subscribe key.
If you are going to both Publish and Subscribe, provide both the publish key and the subscribe key in the connection initialization code, otherwise you can use one or the other respectively.
You’ll need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Once you have, clone the GitHub repository, and enter your unique PubNub keys on the PubNub initialization.
For JavaScript you include the PubNub SDK with a script tag.
We are using the Demo AppKeys, you would replace that init section with your own AppKey that you get after signing up with PubNub.
To learn more, visit our Quick Start for JavaScript.
Ruby Server Sending Messages on WebSocket
For Ruby you need to install the PubNub gem, and use the require statement.
To learn more, visit our Quick Start for Ruby.
For Python you need to install the PubNub module, and use the import statement.
Subscribe Examples in JavaScript, Ruby and Python
In these examples, I am doing subscribe only, otherwise I would also provide the Publish key in the connection init.
For JavaScript you include the PubNub SDK with a script tag.
We are using the Demo AppKeys, you would replace that init section with your own AppKey that you get after signing up with PubNub.
To learn more, visit our Quick Start for JavaScript.
Ruby Server Receiving Messages on WebSocket
For Ruby you need to install the PubNub gem, and use the require statement.
To learn more, visit our Quick Start for Ruby.
Python Server Receiving Messages on WebSocket
For Python you need to install the PubNub module, and use the import statement.
To learn more, you can visit our Python Github Repository.
Try it out!
It’s so easy to send and receive messages, trying it in different languages is very trivial. Looking forward to seeing what you make,
Get Started
Sign up for free and use PubNub to supercharge your
WebSockets or Long Polling app!