Quad6
Quad6

Reputation: 385

Simple/efficient approach to polling with PHP/JS/MySQL

I have a simple web app which lists a private group of people and the last message they posted.

I'm currently just polling by using Ajax to hit a php script every 30 seconds, which gets all people and latest messages then returns as JSON. I parse that and update the DOM.

Not very efficient since most of the time, nothing has changed but it still gets all data each 30 seconds.

  1. Is there anything basic I can do, just with the code to improve this?

  2. Should I use something like pusher.com?

There are a lot of tutorials out there on implementing long polling but I'd like to keep this simple as possible.

Upvotes: 2

Views: 1797

Answers (3)

Ayush
Ayush

Reputation: 42440

In such a case, a better solution would be to use XHR Long Polling. It works somewhat similar to what you're doing right now, i.e. by making AJAX requests.

Here's how it works:

You make the AJAX request as you right are now.
The server side script only returns (echoes) when there is an update. If there isn't one, it keeps looping and waits for an update. This keeps the AJAX request pending.
When there is an update, your server side script returns, your client side JS updates the DOM, and immediately issues a new AJAX request.

PS - For this to work, you'll have to make sure the script isn't set to timeout after 30sec.

In psuedo-code, this is what your server side script will look like

$newInfo = false;

while ($newInfo === False) {
    $newInfo = getNewInfo(); // a method that returns the new info. False if nothing has changed
    // sleep here for a bit
    usleep(500000);
}

// will only reach here when something has changed
echo json_encode($newInfo);

Upvotes: 0

Aamir
Aamir

Reputation: 5440

Without having to change much of the infrastructure, you can return a simple message indicating whether anything has been changed or not.

So, if http://localhost/example/request is returning:

{
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}

on each request, you can instead return the following if nothing has been updated:

{
    "updated": false
}

Additionally, you can also have updated: true indicating it's been updated:

{
    "updated": true,
    "phoneNumbers": [
        {
            "type": "work",
            "number": "111-222-3333"
        }
    ]
}

Overall, all you will have to do is check for the updated property of the returned object on each query. You only need to parse the response if updated is true

In the grand scheme of things though, the overhead caused by each HTTP request. Every time you poll, a new request is made to the browser. Having a lot of concurrent users will start introducing performance issues. I would suggest looking at real-time web frameworks such as Node.js (as viyancs mentioned) as they maintain a persistent connection for each user to allow "pushing" data (rather than polling) hence reducing the HTTP overhead.

Upvotes: 0

viyancs
viyancs

Reputation: 2339

when you use request http every 30 second it's possible many resource will be use, if there are 1000 users and more i think it's not good for web server,

i have suggestion using Nodejs , node js is javascript server a platform built on Chrome's JavaScript runtime for easily building fast,and support longpolling ,and non block request.

with nodejs you can build your webserver that can be handle many users and for realtime application.

there are many framework can be implement with node js

  1. socket.io
  2. express

and this is simple tutorial if you want to try that.. http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial

Upvotes: 2

Related Questions