SignalR was created to solve exactly the problem you are describing.
Here's your situation:
You want a way to display information that could change constantly. At any point, the most up to date information should be displayed.
A Web Forms centric approach would be to use an UpdatePanel. But myself and some others don't really like those. They usually cause problems later on down the road, because they don't play well with other technology. They're also "expensive", they send a lot of data back and forth between the client and server, using up bandwidth and resources.
Another approach, somewhat touched on by deostroll, is to use AJAX to poll the server at a regular interval for the data. This isn't a bad approach, though I would not have implemented it the way he did. His/her way would be a massive drain on bandwidth and resources because you're recreating the entire table every few seconds. Boiled down to a conversation type format, his/her approach look like this:
Client -> Server - Send me the entire table.
Server -> Client - Here's a 1MB table.
Client -> Server - Send me the entire table.
Server -> Client - Here's an 1MB table.
Client -> Server - Send me the entire table.
Server -> Client - Here's a 1.5MB table.
Client -> Server - Send me the entire table.
Server -> Client - Here's a 1MB table.
Instead, an AJAX polling based approach should look like this:
3:30PM Client -> Server - The last data I have is from 3:30PM. Got anything new?
3:30PM Server -> Client - No.
3:31PM Client -> Server - The last data I have is from 3:30PM. Got anything new?
3:31PM Server -> Client - No.
3:32PM Client -> Server - The last data I have is from 3:31PM. Got anything new?
3:32PM Server -> Client - No.
3:33PM Client -> Server - The last data I have is from 3:32PM. Got anything new?
3:33PM Server -> Client - Yes, two new records. Here you go, 10KB.
3:34PM Client -> Server - The last data I have is from 3:33PM. Got anything new?
3:34PM Server -> Client - No.
That uses much less bandwidth. I'm not going to bother showing you how to do it in code, though understand it's relatively straightforward and represents a massive improvement over deostroll's approach.
Instead, I want to describe how a SignalR based approach would work. SignalR is "real time". It takes advantages of several techniques for "pushing" changes from the server to the client. One technology used to implement this is called web sockets, which is the preferred method. But if the client or server isn't capable of web sockets, SignalR gracefully switches to other techniques. You don't have to worry about that though, it's all taken care of for you.
Let's look at a simple way to implement this in SignalR. Every time you change the table data, you want to run a JavaScript function on the clients to update them with the latest data.
First, we need a hub on the server.
public class TableHub : Hub
{
}
On the client side, do this to wire up an event:
$(function () {
var tableHub= $.connection.tableHub;
tableHub.client.tableChanged= function (html) {
//append the html markup to your DOM
};
});
Then whenever the table data changes, call this code on the Server:
var context = GlobalHost.ConnectionManager.GetHubContext<TableHub >();
string html="<table><tr><td>Some data</td></tr></table>"; //obviously you'd built the HTML table up here
context.Clients.All.TableChanged(html); //call tableChanged() on the client!
That will actually end up calling the tableChanged()
function on the client side in near real time, initiating it from the server side! That's basically Remote Procedure Call. So not only is this taking care of selecting the best available transport mechanism (web socks, Server Sent Events, Long Polling) but it resolves client side functions and lets you call them by using dynamics
on the server side.
That was a very basic example. We're sending the entire HTML for the table down, which isn't ideal. But, with a little work you could have the client notified of when to make an AJAX call to an API to retrieve only the changed table data, instead of retrieving the entire table. In fact, that's an approach I took on a website I recently created to push new articles from the server to the client in real time.