Reputation: 3105
I'm currently building a website (ASP.net c#) and we're looking at creating a live feed (similar to the facebook one) for data events on our website. I wanted to know your thoughts on how best to implement this as I am not sure my understanding is correct.
We have events such as: new article published, user coming online.
Users will log in to their members area and should then see the live feed.
My current thinking is that I'd have a query every few seconds (from the users browser) which looks for any new events since the last request date. If there are new ones since dateX (last request date) then show that.
I'd save the last request date along with the userID so when they come back, I know what the last thing they've seen is.
Is this the best way of doing it - or should I be looking at some sort of push methods? I need it to be cross browser friendly and supporting older browsers... so the HTML 5 methods I've looked at aren't really an option.
Thanks for any advice.
Upvotes: 4
Views: 3286
Reputation: 70776
You can use AJAX
for this. ASP.NET has controls such as an Update Panel which would provide you with the functionality you require. You could then set the Update Panel to reload the content based upon the UserId and the last date it refreshed the data.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="UserLiveFeed">
<asp:UpdatePanel ID="upLiveFeed" runat="server">
<ContentTemplate>
<asp:Label runat="server" Text="Last updated" id="lblLastUpated">
<!-- Your content here -->
<asp:Timer runat="server" id="tmrLiveFeed" Interval="3000" OnTick="tmrLiveFeed_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Then update the contents of your feed (Connect to the database and get the new data): (I'm updating the label with the current time every 3 seconds)
protected void tmrLiveFeed_Tick(object sender, EventArgs e)
{
lblLastUpdated = DateTime.Now.ToString();
}
Another alternative would be to use SignalR which is a library for ASP.NET that provides real time functionality.
Upvotes: 2
Reputation: 218862
You may want to to look into SignlarR. This is .NET library to build interactive web applications like what your scenario is.
Another options is what you mentioned in your question. Make an ajax call every n seconds to check the database and update the UI. But I think SignalR is much better than that as it don't rely on the continuous hit approach !
Upvotes: 1
Reputation: 4632
I would suggest you to have a look at some messaging bus freameworks like NServiceBus or the open source MassTransit. These sit on top of the (Microsoft Messaging Queue) MSMQ to make working with it more comfortable,
Instead of polling the database every few seconds you could send a light-wheight message to all subscribed clients only when there are changes written in the DB. The clients then react only to those changes they are interested in. Also you can make the queus transaactional so you do not lose any messages even if the clients are offline/ signed off.
(Edited to make text readable again...)
Upvotes: 0