4

I put together an AJAX chat a while back ASP.NET MVC and jQuery. The javascript would hit the server about every 7 seconds to check for new messages. Obviously this was horrible on performance as the chat grew and included more and more users. The site traffic grew exponentially with so many requests going on. A user could leave the computer on all day and not even be there and they would still be making hits every 7 seconds.

Is there a better way to do this? I have heard of something called "push" but I haven't really been able to wrap my head around it. I think I just need pointed in the right direction.

1.) What is the best way to develop an AJAX chat and have it be scalable?

2.) What is push and how would I just that with jQuery?

CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • You should use something like [COMET](http://en.wikipedia.org/wiki/Comet_%28programming%29) technique –  Oct 18 '11 at 12:22

4 Answers4

1

Server push is a technology that allows the server to push data back to the client without forcing client to make many requests (like every 7 seconds). It is not really a matter of javascript but rather good server scripting. The upcoming HTML5 will make it simple due to server-sent events and/or WebSockets. This will be a true TCP connection between different machines.

But if you intend to make a webpage compatible with older browsers, then the most common technique is the long polling. The client sends request to the server and the server does not respond to it until it has new data. If it does then the response is made and the client immediatly after receiving data calls the server with new request. In practice however this requires the server to be well-written (for example it has to maintain thousands of idle requests at the same time) and can become a rather big challenge for developers.

I hope this helps. :) Good luck!

freakish
  • 54,167
  • 9
  • 132
  • 169
  • This sounds like IIS and ASP.NET would have a hard time maintaining many idle requests. – CatDadCode Oct 17 '11 at 21:33
  • I can't really say, because I'm nginx/Apache with Python/Django developer. But I worked with long polling before and yes, it was really difficult to make it work with thousands of requests. – freakish Oct 17 '11 at 21:39
  • +1 - WebSockets and fallback are the way to go and scaling thousands of persisten connections isn't easy. – leggetter Oct 18 '11 at 12:21
1

1.) What is the best way to develop an AJAX chat and have it be scalable?

I agree with @freakish about the complexity and potential lack of scaling of IIS.

However, there is a relatively new Microsoft option in the works called SignalR which could become a core part of ASP.NET. More details in this related SO Question:

2.) What is push and how would I just that with jQuery?

Partially answered elsewhere, but it's a long-held persistent connection between the server and the client which means the server can instantly 'push' data to the client when it has new data available.

jQuery does support making AJAX requests but the core library doesn't support expose ways of doing HTTP Long-Polling or HTTP Streaming. More information in this SO answer to 'Long Polling/HTTP Streaming General Questions'.

Community
  • 1
  • 1
leggetter
  • 15,248
  • 1
  • 55
  • 61
1

The technique you should use is the real-time persistent long running connections over a web page using WebSockets. You can use this library.

Haythem Tlili
  • 566
  • 3
  • 10
0

Node.js is becoming quite popular to build something like this and supports socket connections so you could push the data out only when there is a new message. But that would be learning something completely new.

Another nice potential would be to use MVC's OutputCacheAttribute and use the SQL dependency option so your AJAX page could be cached and would only be a new request when a new chat message appears. Also you would want your controller to be an Asynchronous controller to help reduce the load on IIS.

Enjoy, optimization is always fun and very time consuming!

endyourif
  • 2,186
  • 19
  • 33