long-polling


Long polling confusion


I've been learning about long-polling and after reading a little about it, find myself a bit confused. I looked at explanations here and here
I don't quite understand what the point is in making the server sleep prior to responding. I know the idea is to try to keep the connection open between the client and server, but in the 2nd link with the highest upvoted php example, it has a code snippet where the server just calls sleep for some amount of time.
<?php
/* Send a string after a random number of seconds (2-10) */
sleep(rand(2,10));
echo("Hi! Have a random number: " . rand(1,10));
?>
What exactly does the sleep accomplish? Doesn't that just make it so that every time you send a request, it will simply sleep first (making the server do nothing), then respond, making it exactly like a standard http request but much slower? I don't see how it helps to maintain a connection if the sleep just makes the server do nothing for some amount of time. Isn't the point of long polling to respond to the client whenever new information is received?
Long polling is about the server not responding until new data is ready to be returned to the client (not sleeping for fixed durations of time).
Assume you need to get a new piece of information from a service. Your (polling) choices are:
Regular polling The client sends a request in fixed intervals and requests fresh information. This is simple to implement (and potentially easy on the number of concurrent connections), but it means 1) you need to tune the interval for your use-case (how soon you need the data after it's ready), 2) you need to wait a maximum of (RTT + delay interval) for your data, 3) there is a large number of handshakes that take place
Long polling The client send a request and waits for a single response. The server thread will sleep and will be notified upon the data being available, at which point it will return them as part of that response. This means that the client will have the data in RTT / 2 time after being available. But the server needs to keep many more concurrent connections open.

Related Links

Koa 2 router how to make a long poll request
Confluent long polling properties in kafka-rest
How to combine async and blocking requests in VuGen?
Long polling confusion
WAS Liberty & Atmosphere long-polling 1 min delay
Long Polling interval in atmosphere
MQTT long polling over GPRS/HSDPA
JS and CSS file fails to load when the page is refreshed in grails application which uses Atmosphere Meteor plugin
Security concerns in long polling
Kafka long polling
How to build the request for a long poll url for box.com
how to access enterprise events using Java code
Handle client timeouts when serving long polling with gevent
Long polling blocking multiple windows?
Long polling on a penny auction site?
Cannot implement long polling

Categories

HOME
xamarin
wso2-am
asp.net-core
reserved
google-docs
react-redux
spring-jdbc
jgroups
icloud
ezpublish
convolution
amazon-cloudformation
medical
nano-server
spring-kafka
imacros
propel
textfield
ml
tar
kudan
bootstrap-tour
crosstab
buildbot
tdd
spark-jobserver
minitab
buck
directx-10
windows-dev-center
uiswipegesturerecognizer
language-concepts
git-merge
automake
neuroscience
service-discovery
android-fingerprint-api
lumberjack
android-tabhost
eclipse-scout
jsch
smartcontracts
chain-builder
worksheet
sage-one
return-value
nstextview
knockout-components
pg-dump
np-complete
dwscript
azure-sdk
libpng
firebaseui
clang-static-analyzer
react-native-listview
asp.net5
tcpserver
r-tree
collapse
simple-framework
android-listview
sysinternals
nsight
geonetwork
gadt
winddk
issuu
rebol3
stxxl
gridfs
listings
soundtouch
buster.js
simba
cisco-jtapi
pysimplesoap
php-parser
gnu-prolog
gcj
mod-auth
mirah
user-friendly
multi-tier
compiler-specific
webkit.net
project-hosting
spec#

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App