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
java
ibm-bluemix
bluetooth
coq
hook
server
tizen
json.net
alfresco
android-4.4-kitkat
google-oauth
packages
add
amazon-cloudformation
kibana-4
modx-revolution
collectd
windows-azure-storage
spring-tool-suite
size
netflix
clearcase-ucm
foselasticabundle
propel
code-review
excel-vba-mac
contextmenu
jprofiler
jtextfield
accessor
realex-payments-api
language-agnostic
trading
smb
qhull
status
react-css-modules
uninstall
dbclient
large-file-upload
jaxb2
gsoap
google-rich-snippets
frame
scorm2004
http-get
stringtemplate
subset-sum
serverside-rendering
streamsets
greenrobot-eventbus
ruby-on-rails-3.1
nomethoderror
eigenvalue
bootstrap-dialog
heightmap
netcdf4
qwt
network-flow
jlink
rdfs
tropo
media-player
arrow-keys
gcsfuse
imanage
jquery-filter
firmata
android-fonts
spring-android
vmware-tools
httplistener
adxstudio-portals
nsfilemanager
plottable.js
player
clang-static-analyzer
freelancer.com-api
freedesktop.org
iad
intel-fortran
atk4
computer-algebra-systems
kcachegrind
tarjans-algorithm
php-ci
sysinternals
sniffer
typekit
asp.net-dynamic-data
fpml
wordpress-theme-customize
p4java
braille
cloud-connect
buster.js
back-stack
jquery-mobile-dialog
itmstransporter
path-separator
frameset
cascalog
custom-backend
rfc1123
yetanotherforum
w3wp.exe
sproutcore-2
mirah
swing-app-framework
phonon
putchar
wsdl.exe
ntvdm.exe

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