The Daily User
The Daily User

Reputation: 11

Best Practice to check SQL Database for a change in ASP.NET

I have a webpage that pretty much needs to be refreshed as soon as a value in the database changes. I see a lot of Reverse AJAX features that will keep a session running.

My main question is, what do you think I should use?

My other question is... can't I just do an infinite loop in javascript and just call a webmethod that will check the database? Why would I need the comet sockets or web service or anything like that?

Upvotes: 1

Views: 82

Answers (1)

Lunster
Lunster

Reputation: 906

The old-school solution is to implement a "heartbeat", where every 15 seconds or so (this can be changed obviously) a lightweight AJAX "HEAD" request is made, and the server-side controllers would indicate in the reply header that there is content to be received.

The application can than make another request for further data. Note, you can just do this in one request, and that works fine too - especially if your data is likely to change fairly often.

This is essentially what you described as an "infinite loop", but that's a misleading term. It's often achieved using repeated setTimeout() calls - do not use setInterval(). You want to wait for a response or a timeout before sending another request, or things could get complicated and cause you headaches.

Consider that the requests to the server will quickly multiply with the amount of traffic this application is getting. Each request should be secure, but lightweight. The server handling the request should implement a form a of caching, you do not want to be making a database call every heartbeat request.

This old-school method works, I've seen it used everywhere. There are newer technologies available to developers though:

One newer method is using WebSockets, but since you're using asp.net, I recommend you look into Microsoft's SignalR, which combines several technologies and ensures compatibility on older browsers/devices.

Upvotes: 1

Related Questions