Search code examples
phpajaxhttppollingajax-polling

AJAX/PHP Why is HTTP-Polling so laggy?


Why is HTTP-Polling so laggy?

What I have is a button, and whenever a user clicks it a MySQL database field gets updated and the value is displayed to the user. I'm polling every 800 milliseconds and it's very laggy/glitchy. Sometimes when clicking the button it doesn't register it. And I actually need to be polling quite a bit more frequent than every 800 milliseconds.

This is also with just 1 user on the website at a time... When in the end there is going to be many at once.


Solution

  • HTTP-streaming/Long-polling/Websockets instead of polling

    When you need real-time information you should avoid polling(frequently). Below I would try to explain why this is wrong. You could compare it to a child in the back of your car screaming every second "are we there yet" while you are replying "we are not there yet" all the time.

    Instead you would like to have something like long-polling/HTTP-streaming or websockets. You could compare this to a child in the back of your car telling you to let him know when "we are there" instead of asking us every second. You could imagine this is way more efficient then the previous example.

    To be honest I don't think PHP is the right tool for this kind of applications(yet). Some options you have available are:

    hosted solutions:

    • http://pusherapp.com:

      Pusher is a hosted API for quickly, easily and securely adding scalable realtime functionality via WebSockets to web and mobile apps.


      Our free Sandbox plan includes up to 20 connections and 100,000 messages per day. Simply upgrade to a paid plan when you're ready.

    • http://beaconpush.com/

      Beaconpush is a push service for creating real-time web apps using HTML5 WebSockets and Comet.

    host yourself:

    • http://socket.io:

      Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms

    When becoming very big the "host yourself" solution is going to less expensive, but on the other hand using something like pusherapp will get you started easier(friendly API) and also is not that expensive. For example pusherapp's "Bootstrap" can have 100 concurrent connections and 200,000 messages per day for $19 per month(but when small beaconpush is cheaper => do the math :)). As a side-note this plan does not include SSL so can not be used for sensitive data. I guess having a dedicated machine(VPS) will cost you about the same amount of money(for a simple website) and you will also have to manage the streaming solution yourself, but when getting bigger this is probably way more attractive.


    Memory instead of Disc

    whenever a user clicks it a MySQL database field gets updated and the value is displayed to the user

    When comparing disc I/O(MySQL in standard mode) to memory it is extremely slow. You should be using an in-memory database like for example redis(also has persistent snapshots) or memcached(completely in memory) to speed up the process. I myself really like redis for it's insane speed, simplicity and persistent snapshots. http://redistogo.com/ offers a free plan with 5MB of memory which will probably cover your needs. If not the mini plan of $5 a month will probably cover you, but when getting even bigger a VPS will be cheaper and in my opinion the prefered solution.


    Best solution

    The best solution(especially if you are getting big) is to host socket.io/redis yourself using a VPS(cost money). If really small I would use redistogo, if not I would host it myself. I would also start using something like beaconpush/pusherapp because of it's simplicity(getting started immediately). Hosting socket.io(advice to play with it on your own machine for when getting big) is pretty simple, but in my opinion more difficult than beaconpush/pusherapp.