1

Even though working on PHP for so long, recently came to know about long polling, otherwise I was sending periodic ajax.

It is understood that sending periodic ajax consumes bandwidth ( Considering the case if site have thousands of users). As well server remains busy to serve periodic ajax requests.

Advantage of long polling is that it drastically reduces the bandwidth as ajax responses only when there is change, Unless ajax remains open. However server need to keep on working ( kind of while loop until false condition) until some changes has happened.

My question is, In this type of technique, server has to incur load. Wont it affect the say way as periodic ajax when there are thousands of uses?

Sorry If I am wrong.

Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62

1 Answers1

3

When you say "load", let's consider bandwidth, CPU time, and other resources.

Bandwidth

As you say, periodic ajax consumes bandwidth. There will be an HTTP request-response pair for each poll, even if the response from the server is basically empty. With long polling, the server doesn't respond unless it has something to say. This is where you save bandwidth.

CPU

If your long-polling implementation uses a sleep() on the server side, it will not use many CPU cycles.

For PHP:

Note that in general, you should use a sleep()-like function any time you want to delay in software. Do not use a tight loop without a sleep() for a timed delay.

Other

Depending on the configuration of your server, each active user will probably use a process or thread. Even while it is sleeping, this process or thread will consume some resources, including any allocated memory. Unless you're talking about a huge number of simultaneous users, or your application uses a lot of memory on the server per user, you're unlikely to run into hardware limitations. You might run into a soft-configured limit on the number of threads or processes first.

So, if your application holds a very large amount of memory per active user, long-polling could make you memory-bound with fewer users than a periodic AJAX implementation which allocates and deallocates resources per ajax hit. With good design, this shouldn't be a problem though.

bsa
  • 2,671
  • 21
  • 31
  • @bas, at first thank you. CPU: **Do not use a tight loop without a sleep() for a timed delay.** does it mean `while($condition){ sleep(10);// $condition = some logic }`? Secondly When long pole xhr consume some resource, they will remain occupied by thread untill server respond. Now what if long pole xhr comes form other users for same resource? Race condition ? Deadlock Right? Then??? – Rajan Rawal Oct 25 '13 at 08:57
  • Yes, the while loop you wrote is what I meant. Deadlocks and race conditions are a different issue. That's down to correct implementation of resource locking between threads/processes. If you implement correct locking, you should avoid contention issues, but be aware that the critical section (where the resource is locked) may become a bottleneck for your application, so don't lock the resource longer than necessary. – bsa Oct 25 '13 at 09:11
  • In short it is fact, that long polling as compared to periodic ajax, efficiently reduces bandwidth but makes server busy. Consider the case where long polling xhr sent and server has nothing to respond for a long? It will keep server busy right? Does Facebook uses long polling for notification queue or socket.io? – Rajan Rawal Oct 25 '13 at 09:20
  • I disagree that long polling inherently makes a server busy compared to regular ajax. A poor implementation could do so, but that's true of any technique. I don't know what Facebook uses, but Socket.IO can use a variety of underlying methods. Generally it uses WebSocket, but can fall back to long-polling when WebSocket is not available. Long-polling is just a technique using plain HTTP when something better like WebSocket is not supported. – bsa Oct 25 '13 at 09:33