What is Subscribe Long Polling?
When a client subscribes to a channel using PubNub, it establishes a long-lived HTTP connection to listen for messages. This process, called long polling, works as follows:
- PubNub SDK sends a request to the PubNub network with a list of channels to subscribe.
- The request remains open until a message is received on any of the subscribed channels.
- When a message is sent to one of those channels, PubNub responds with the message data and a 200 OK status.
- After receiving a response, PubNub SDK immediately issues a new subscribe request to continue listening for messages.
- If no messages arrive within 280 seconds, PubNub responds with an empty response and a 200 OK status, and the SDK initiates a new subscribe request.
This cycle repeats continuously, ensuring the client always has an open connection to receive messages in real-time.
Confusing Subscribe Long Polling with Latency
It is possible to confuse the pending state of a subscribe request with a latency or delay. This happens because:
- A subscribe request is not an immediate-response API call. It remains open (pending) until a message arrives or the connection reaches 280 seconds and the cycle restarts.
- If a message arrives 45 seconds after the request was made, you can mistakenly assume that there was a 45-second subscribe latency. In reality, this is how long polling works—the connection stays open, waiting for a message to arrive.
To learn more, please visit: