Reputation: 9393
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?
Upvotes: 6
Views: 3151
Reputation: 845
Long polling is a great technique but as with any solution the efficacy depends on many variables including hardware setup and hence there are no absolute solutions.
Please be aware that Long polling you are keeping the connection alive which may cause performance issues with many clients.
You solution should take into account :-
You should be a frugal when it comes to ajax. Request & response should be done on a need basis. The success of a ajax implementation will be incomplete without a well thought out caching solution which is more event based rather than request based.
Below is a simplified version of one of the techniques we found useful in a project:-
In our solution we also did dynamic calculation of the interval of the next poll. Keeping the interval dynamic allows the server to control the request. For example let's assume most comments / answers happen in the first 1 hour, beyond that there is no point having the interval time as 1 sec, so the server can increase that to 2,3 or even 5 sec dynamically as the time increases, rather than hard coding the interval as 2 sec. Similarly the interval time can be decreased if there is flurry of activity in an old post.
We also checked for idle clients and other things.
Upvotes: 0
Reputation: 4842
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.
Upvotes: 10
Reputation: 47189
Just my two cents but I have previously solved a similar problem by having 2 webservices. Basically one with a method along the lines of HaveNewData()
and another GetData()
(In my case I had a number of webservices I wanted to call).
So basically call HaveNewData()
on a regular basis (I think in any case every 2 seconds is a bad design and unnecessary) and return a simple 0 or 1 for this method (minimal data). If HaveNewData()
returns 1 then make your expensive webservice call. Alternatively you could also just return a null value in the primary webservice when no new data is available and in my experience this scales at least "pretty" reasonably.
Upvotes: 0