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
arduino-uno
testng
oracle11g
mfc
smarty
json.net
xmpp
ip
disassembler
swagger-ui
pheatmap
webrequest
fancybox-3
volttron
timeout
vb.net-2010
msp430
iolanguage
openrefine
windows-7-x64
evopdf
interop
scichart
grails3
conemu
arabic
trading
android-widget
sqlcipher
apache-commons-io
opentype
dbclient
jaxb2
google-cloud-endpoints-v2
windows-dev-center
android-kernel
ghost4j
normal-distribution
gesture
serverside-rendering
libvpx
az-application-insights
ansible-playbook
dism
eclipse-scout
ibpy
nomethoderror
spring-security-kerberos
executenonquery
cubic-spline
menuitem
user-accounts
filepicker
forever
python-cryptography
memory-alignment
zendesk-app
word-vba-mac
xcb
thrust
bstr
qgraphicsview
livequery
asp.net5
itextpdf
natvis
rtbkit
lib.web.mvc
tmuxinator
createprocessasuser
mmc
truevault
fscommand
operator-precedence
c3
device-manager
neolane
expected-exception
hyprlinkr
industrial
gridfs
ruby-datamapper
ftps
dataservice
sabredav
http-unit
distutils
anonymous-methods
trusted
rdoc
regsvr32
mysql-error-1005
swing-app-framework
procedural-music
grid-system

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App