I don't know which is the most efficient way to do. I am seeking a better algorithm.
Let us take some example like Facebook when user posts a post, it will be updated to his friend without any page refresh and we know its by ajax request. But how can we know that some one has posted a new thing? may be like putting a timer for every 2 seconds and sending an ajax request for some table and checking if some user posted something. Right?
But is there a way to do without setting a timer because performing the operation for every 2 seconds may cause severe server issue I think so? Just want to know if there is a better way instead of setting a timer?
Currently what Facebook and Google employ, is a technique called long polling.
It's a simple system whereby the client makes an AJAX request to the server. The server takes the request and checks to see if it has the data the request needs. If not, the request is left open but deferred by the server. The second the server has the data, the request is handled and returned to the client.
If you open up facebook, you'll see requests being posted to Facebook which take around 55 seconds to complete. Same goes for Gmail and a few other web applications that seem to have some kind of push system.
Here's a simple example of how these requests might be handled:
Client:
timestamp 0
Server:
timestamp 0
by checking the timestamp of the data on the server. Lets say the data on the server has the timestamp 234
. Client:
timestamp 234
.Server:
timestamp 234
with the current stamp of the data on the server. timestamp 235
.You can read a more in-depth explanation of more modern mechanisms for live updates.