I have a hybrid web app for android. The webpage or website the app shows is in asp.net. The javascript for the webpage running in the webview of the app is sending out an ajax request every 10 seconds to an asp.net page which queries a database server for refreshed data and returns the needed values as a result of the ajax call. Note, this is expected to be a very high volume site.
Often the data will be the same. I am considering using push notifications instead of the page having to poll every 10 secs, so that data is only sent when it is different. I am looking into what is called SignalR to do this and it doesn't look that bad.
What I want is a solution that is the least overhead to the web server (IIS8) and the database server (Sql Server 2014). So if push notifications are sent out, I think the web server would have to keep track of each launched instance of the webpage as a session rather than every page instance being static and stateless as it normally is.
So what does this mean to the web server and database server in terms of using up processing time and resources? Is it more efficient for the web server to create and manage all of these sessions than it is for the web server and database server to handle polling requests from everyone?
To be clear, imagine polling for a delivery driver's location and the driver's stops. The driver's stops might not change at all or only occasionally, but the driver will change every time his or her location changes. Should I poll for all info every 10 secs so that any changes are guaranteed to be picked up, or should only the info that needs to be sent be sent out and only when it needs to be sent?
Thanks in advance for your opinions and advice.
Best Answer
Events and Subscribers
When you make the shift from polling to event notifications, the best way to think about it is in terms of events and subscribers.
In your concept, you imagine the web server maintaining a session state for each instance of the client, and effectively polling the database on its behalf in order to know when to send the notification to the client.
You are right to be concerned about the impact on server resources of this approach! You haven't yet fully leveraged the power of notifications, you've just shifted where the polling happens.
A more effective way is for clients to register themselves to receive a notification when a particular piece of data changes. Then, when that data changes, publish a notification that the data has changed. Let the notification framework identify who has registered to receive a notification (subscribed) and to let them know.
One way to do this with signalr is through 'groups'. Exactly how it should be structured depends on your data model, but using your example, you could have a group per driver. A simplified view of the sequence:
This way you receive notifications on the client, without polling anywhere.
Edit to add - Efficiency
Following your comment, a few notes on efficiency using your example of a chat application:
With polling approach, you are using the following resources:
Scalability:
With a signalr notification approach, you have:
Scalability:
Based on that, I think the notification approach is more efficient. As you can see, while signalr will maintain the group membership list, you have to do that anyway even if you go down a polling approach.
Terminology Note