New GitHub repo: simple php-long-polling for creating real-time apps
I’ve just pushed a sweet little new project into GitHub: A very simple and totally reduced PHP long-polling demo application, called php-long-polling. No installation, just copy, edit and run (you need Apache/PHP running).
Long-polling makes near “real-time” applications possible. So, this means that whenever “data” is updated on the server, the client will INSTANTLY be updated. For the end-user it feels like “real-time”.
The technique behind this is really simple: Usually, in an “always very up-to-date” application the client ask the server for new data in intervals, let’s say once per minute. This might work, but bombs the server with requests. Just imagine you have 10.000 people on your site, everyone sending requests permanently, once per minute. That’s (averaged) 167 per second.
With long-polling, the client sends a request – and gets no direct answer. The client-server connection is kept open, the request still pending. This can take hours. When the server has “new data” suddenly, the requested script (might be a simple .php) delivers the data, the client gets the data, the connection is closed… and the whole thing starts again until there’s new data.
This offers excellent possibilities for near real-time application or passive monitoring. For basic apps, we don’t even need special tools, a simple .html, simple js. and a simple .php is everything you need.
The right webserver
Biggest issue! When having a lot of connection to clients open, the Apache server will create a thread (?) for every request – which results in a quite large usage of RAM. As the casual apache thread is 15 MB, this might sum up dramatically quite fast. There’s a lot of opposing opinions on that on the web, I’m still not sure what to use, some people say NGINX and lighttpd are perfect, some say node.js is the only way to go – but that’s a totally different topic. Appserver.io also seems promising.
Has anyone here experiences in scaling long-polled js/php applications ? What’s the way to go when it comes to servers ? Feel free to comment…