Server-Sent Events vs AJAX Polling vs WebSockets for Notifications - c#

I am building simple notificiation system and I just wanted to know what is the best technique to use to do the job, right now I am doing AJAX requests to server every 30 secs per user to check for new notifications.Since this will be entertainment site it is not crucial for notifications to be pulled in realtime. My main concern is browser support, conserving server resources and scalability, so which technique would best fit the job?

Try SignalR
http://www.hanselman.com/blog/AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR.aspx

Related

How would I run a continuous task on an ASP.Net Server?

I've tried researching this, but haven't found much that sounds similar to something I'm needing to implement. In short, we'll be running an ASP Website on a server that will be accessed by clients. Ideally, we have a function that we want to initialize upon the start of a user's session, and stop when the session ends. While the session is happening, this function sends and receives messages via socket communication, meaning we need to access the send/receive functions of this class from pages in order to move information. What's the best way to go about this?
Look into SignalR. That's probably what you're wanting. Its "hubs" are effectively what you're looking for to spin up on session initiation, and spin down when the user disappears. It has a client-side JS library that automatically chooses the best connection method available (e.g., websockets > server-sent-events > long-polling), and it allows you to send messages both from the client to the server, and from the server to the client.
http://www.asp.net/signalr
Another alternative that I've played around with in the past is XSockets:
https://xsockets.net/
It's similar to SignalR in many respects, but it's not free.
It's hard to tell from you description, are you looking to communicate with the client browser via sockets? Or are you trying to communicate with some other service via sockets?
Web applications are not ideally suited for deterministic types of actions. It's difficult for the web server to know whether or not the client has actually closed their browser or not. In most cases, sessions simply time out after a period of inactivity (20+ minutes in most cases). So you cannot reliably know when the users session has actually ended.
To top it off, there are certain edge cases where Session_End will not fire. For instance, if the app pool recycles, then no Session_End event will fire. This may not be an issue, since if the app pool recycles your other connections would also recycle, but it's still an issue to keep in mind.
Finally, Web apps are not intended to be long running.

Trying to avoid Traffic on Server

I am using C# ASP.NET MVC 4 Razor
I have a Grid in ASP.NET MVC Razor View that displays the user records. Is there any way to show the new users in Grid without sending the async request to server after each 1 min ?
I searched on Google a lot. Now, finally I am posting the query here to get any clue for this solution to avoid Traffic on Server. As this page will be visible to at least 20,000 users
#Christos approach is the right one!, Just to add more info about it for an ASP.NET solution I would it use SignalR, that lets you implement a simple server/client communication and it's cross browser (it has several polyfills that if it cannot use web sockets it will use server-sent event, and so on), and the best part is that you don't need to worry about that implementation.
Once your clients are connected to the signalR server you can notify them everytime you need to add a new item to the grid.
http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr
I hope it helps!
You could make use of the the publish/subscriber pattern. This can be done using for instance a redis server.
In software architecture, publish–subscribe is a messaging pattern
where senders of messages, called publishers, do not program the
messages to be sent directly to specific receivers, called
subscribers. Instead, published messages are characterized into
classes, without knowledge of what, if any, subscribers there may be.
Similarly, subscribers express interest in one or more classes, and
only receive messages that are of interest, without knowledge of what,
if any, publishers there are.
Please have a look here.
Doing so, the first time a client requests data from the server will subscribe to the server for taking any updates. Once any update arrives, the server will push the update to the clients that have subscribed, without requiring from the client to make any other request to the server.
As for implementation details, please have a look here.

Is there any way to look for new messages in server without polling?

My project is a ASP.NET MVC3 application hosted in the Azure environment. I want to show new messages for a user as it arrives without reloading the page. I can use Javascript to do polling at regular interval and pull messages from server, but my client is worried that it will make their server slow if 10000 users poll at some interval.
I thought (not gone too much into) of using Microsoft push notification service, but I think it can be used only with windows phone.
Is there any way i can do push messaging?
Options are:
SignalR
Websockets
Likely you want SignalR in your application. You can grab it as a Nuget package:
Install-Package SignalR
The definition of SignalR: Pushing data from the server to the client (not just browser clients) has always been a tough problem. SignalR makes it dead easy and handles all the heavy lifting for you.
Something else: you're on Windows Azure. You can always scale out/up if needed. The new preview portal makes that dead simple when making use of Windows Azure Web Sites.
I've had much success with Pubnub: http://www.pubnub.com/
You can easily push messages to javascript or c# based clients. They have a generous free account to try out. It was much less work to set up and scale horizontally than SignalR.

Long polling for real time messages/updates

What is a good strategy for implementing long polling in a .Net application.
Would it mean having a JS setInterval() based approach to keep polling the server for updates that can be rendered on the page. I have always thought that that could be a challenge when it comes to scalability as it seems it would generate a lot of extra requests to the web server. I have read that this type of functionality should be implemented using a non blocking web server (single threaded) NODE.js etc...
Since there's only one thread/event loop it seems like the requests would have to be very lightweight to service several requests in a timely fashion. Can Node.Js trigger db calls?
I have seen an online dating site where you receive notification in the form of a fad-in/fade-out popup when someone visits your profile when you're currently logged into the system. I am impressed that something like that can work so well for a high volume site.
Is it reasonable to assume that this type of notification system is implemented using long polling? Based on constantly polling through JS?
I am seeing similar behind the scenes updates her on the SO site as well (messages/votes etc) Does this use a similar strategy as well?
SignalR and pokein are two good options.
A blog post by scott hanselman which explains using SignalR
http://www.hanselman.com/blog/AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR.aspx
Realtime web applications have been with us for quite some time now:
the history of Polling goes from setInterval Technique to HTML5 WebSockets.
Here you can find Simple Long Polling Example with JavaScript.
http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

Web Chat Application - ASP.NET/Jabber/Ajax/WCF/Comet/ReverseAjax - Issues Faced - Seeking Insights

I've been trying to build a web based chat application for the past three weeks and i'm facing issues with whatever route (programming technique/technology) i take to build it. I've explained the issues i've experienced with all of'em below. Kindly provide whatever insights you have in this.
ASP.NET-AJAX
First issue is that it is Not Really Real Time
If client hits the chat server every x seconds (constant time stamp) it is not going to be real time unless x is very very less
If x is very small like 1 second and if there are 1000 users online at the same time i think it is really going to hammer the chat server and cause scalability/performance issues
WCF-Duplex
I unfortunately wasted considerable amount of time in this trying to build a WCF duplex service which maintains all the clients and invokes the client through the channel as and when required. But i recently learnt that WCF duplex callback wont work with ASP.NET (since http is request and respond type). I was following this great article to build a duplex service.
Comet/ReverseAjax/HTTP Server Push
I'm extremely new to this technique and wonder how well enough it can scale. After my first glance on this programming technique here in wiki and the very first article on Comet by Alex here, i learned that the client always maintains an open connection (long living ajax calls) to the server which can be used by the server to push "interesting events happening in the server" to the browser (client). So how well can it scale? What if the max no of open connections exceed in IIS or any other issues like that?
Jabber Server/Client (XMPP)
I see that most of the prominent chat applications that can be seen online are making use of Jabber. I also learned that writing a Jabber server from the scratch is a tedious task. I have separate user profile store for by application. Can i integrate that with Jabber easily? Any open source Jabber servers that i can host privately? (I've seen many open source tools to build the client easily)
Any insights provided are very much appreciated.
Thank you
NLV
If you are using .NET, check out WebSync. It allows for fully scalable comet using IIS to integrate directly with your application. There is a free Community edition you can try out, along with tons of examples and chat demos.
I just recently implemented a multi-client Jabber web-app using WebSync and jabber-net.
PokeIn provides shared objects among the clients and it simply helps you to create impressively solid and fast web applications. Even if your application is hosted on multiple servers, PokeIn manages the shared objects on all of them. So, this feature will help you to create quite effective solutions. In addition to these, you will find very useful samples over there
I know this is old but if somone new founds this you should consider using SignalR
Open Source Jabber Server
Have you checked out OpenFire

Categories

Resources