What are you in the mood for today?

Saturday, 4 May 2013

As some people seem to be having a tough time understanding this reverse ajax I decided to try and explain. so long polling or reverse ajax as its otherwise known. Is when you simulate the server informing you of changes, instead of constantly pulling the server for updates.

*but Brian why is it a bad thing to constantly poll the server??Polling the server is very expensive as a new connection needs to be created & handled on each request. Combine this with a lot of users and short polling time and your server can get quickly overloaded.Here's an example of a normal ajax request to poll for updates.

What we are doing here is allowing the client to send request to see if anything has changed, then what I short while and the client will request again.

Long polling

This approach is best when you are checking for changes on a somewhat infrequent basis.Here we change the architectural little bit. we take the request from the client.. but if there is nothing to return, we will hold the connection open.-------- --------| Base | | Ship |-------- -------- | || Everything ok? | >| ---------------->|> | | checking.. no change | | (..Wait..)| | checking.. no change| |(..Wait..)| | checking.. no change <|<---------------- |<| Everything ok? | >| ---------------->|> | | checking.. no change| |(..Wait..)| | checking.. There a problem!!!!!<|<---------------- |<| || || || |This approach allows us to better manage connections as each client will have one connection that can be used over a longer time. If something happens with connection the good thing is the network layer will inform you and then you can just reconnect.I have found good approach is to run a loop on the server 30 times and sleep for 1 second each iteration. but your mileage may vary