What is HTTP Long Polling?

3 min read Joe Hanson on Dec 1, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

Web applications were originally developed around a client/server model, where the Web client is always the initiator of transactions, requesting data from the server. Thus, there was no mechanism for...

Web applications were originally developed around a client/server model, where the Web client is always the initiator of transactions, requesting data from the server. Thus, there was no mechanism for the server to independently send, or push, data to the client without the client first making a request.

What is HTTP Long Polling? from PubNub on Vimeo.

In a Nutshell: HTTP Long Polling

To overcome this deficiency, Web app developers can implement a technique called HTTP long polling, where the client polls the server requesting new information.  The server holds the request open until new data is available. Once available, the server responds and sends the new information. When the client receives the new information, it immediately sends another request, and the operation is repeated. This effectively emulates a server push feature. A simple diagram below:

For a technical, in depth look at HTTP long polling and implementations for Python, Ruby and JavaScript, check out our article on WebSockets and Long Polling in JavaScript, Ruby and Python.

Considerations for HTTP Long Polling

There are a couple of things to consider when using HTTP long polling to build real-time interactivity in your application, both in terms of developing and operations/scaling.

  • As usage grows, how will you orchestrate your real-time backend?
  • When mobile devices rapidly switch between WiFi and cellular networks or lose connections, and the IP address changes, does long polling automatically re-establish connections?
  • With long polling, can you manage the message queue and catch up missed messages?
  • Does long polling provide load balancing or failover support across multiple servers?

When building a real-time application with HTTP long polling for server push, you’ll have to develop your own communication management system. This means that you’ll be responsible for updating, maintaining, and scaling your backend infrastructure.

Backend Infrastructure for Real-time Functionality

With these considerations in mind, that's where a real-time communication platform like PubNub comes in. PubNub takes care of the backend infrastructure for you, so you don’t have to worry about maintaining and orchestrating your real-time network.

When looking at HTTP long polling with the goal of streaming data, PubNub is a low-latency and low-overhead real-time Web app communication environment, and features the ability to send messages to a single client, client groups and all clients. Upgrading to PubNub is both rapid and easy since PubNub is based on a publish/subscribe model.

Key Benefits of Protocol Agnostic Real-time Messaging

Instead of relying solely on HTTP long polling for real-time messaging, a protocol agnostic approach is beneficial. PubNub automatically chooses the best protocols and frameworks depending on environment, latency, etc. Any server or client code wanting to communicate makes a single API call to publish or subscribe to data channels.

The code is identical between clients and servers, making implementation much simpler than using HTTP long polling. In terms of connection management, the network handles network redundancy, routing topology, loading balancing, failover, and channel access. Additionally, PubNub offers core building blocks for building real-time into your application.

  • Presence – Detect when users enter/leave your app and whether machines are online, for applications like in-app chat.
  • Storage & Playback – Store real-time message streams for future retrieval and playback.
  • Mobile Push Gateway – Manage the complexities of real-time apps on mobile devices, including Push Notifications.
  • Access Management – Fine grain Publish and Subscribe permissions down to person, device or channel, for HIPAA-compliant chat applications or other sensitive use cases.
  • Security – Secure all communications with enterprise-grade encryption standards.
  • Analytics – Leverage visualizations into your real-time data streams.
  • Data Sync – Sync application state across clients in real time, for digital classrooms and multiplayer game development.

More from PubNub

Building a Core Product Strategy that Meets a Market Need
News Jun 1, 20221 min read

Building a Core Product Strategy that Meets a Market Need

Patrick Ng, Innovation Director at Spark Launchpad, joins Casey Clegg, COO at PubNub, to discuss effective ideation and the...

PubNub Staff

PubNub Staff

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff