Design Patterns – Better Alternatives to Traditional Polling Methods

angularjsdesign-patternsjavascriptpolling

I'm currently in a AngularJS/Javascript environment.

Currently the application using the polling method (i.e, to retrieve new data from server in a fixed amount of seconds).

This is quite taxing and does not retrieve latest result immediately. Assuming the server is using ASMX for webservice functions and if possible, without a major overhaul, how can I better improve on the application efficiency?

Edit 1:

By taxing , I mean the server would have to pull up different SQL tables and associated data and stuff, meaning complex data processing which if there is alot of concurrent users I'm afraid there might be possible issues with the server performance in the future.

The latest data will not be retrieved as currently the application is using request-response https type of calls.

I can improve more on the mobile application side, but not able to modify much on the server side apart from the server's webservice files.

Best Answer

If by "traditional" you mean every client hammering the server as often as they can then I can help at the architectural level.

The appropriate design depends on where your server falls in this grid:

Control \ Resources : Sufficient | Insufficient
Customizable        :     SC     |     IC
Untouchable         :     SU     |     IU
  • SC : Make the server follow the observer pattern. Allow clients to register as observers.

  • IC : Only allow a proxy system to register with the server. Clients register with proxy.

  • SU : Only allow a proxy system to poll the server. Clients register with proxy.

  • IU : Only allow a proxy system to poll the server. Clients register with proxy.

A proxy that follows the observer pattern can solve both customization and resource issues. This works just as well over the internet as is does between objects. When you register you're asking to be called when something happens. Listen on a port and you're ready to be called. ajax, REST, UDP, whatever.

The problem with polling is that the observer controls when the call happens yet doesn't know when state changes so it makes many needless calls. This eats resources especially when there are many observers. The key to dealing with polling is: as soon as possible, get the direction of calls going from the point of state change to the observer. Then calls happen when they're needed. Not when they aren't.

If you have polling calls going this way:

  • Server <=== Many observers

Any of these would be a better way to detect changes:

  • SC: Server ===> Many observers

  • IC : Server ===> One proxy ===> Many observers

  • SU : Server <=== One proxy ===> Many observers

  • IU : Server <=== One proxy ===> Many observers