1

I'm trying to implement a custom live chat program on the web, but I'm not sure how to handle the real-time (or near real-time) updates for users. Would it make more sense to send Ajax requests from the client side every second or so, polling the database for new comments?

Is there a way to somehow broadcast from the database each time a comment is added? If this is possible how would that work? I'm using Sql Server 2008 with Asp.net (c#).

Thanks!

chobo
  • 31,561
  • 38
  • 123
  • 191
  • 3
    Yarrr matey! It be [long-polling](http://stackoverflow.com/questions/tagged/long-polling) that ye scurvy dogs be looking for. – Chris Shouts Oct 19 '11 at 18:17
  • @ChrisShouts: Ah, so this is called long-polling. Learned something today. –  Oct 19 '11 at 18:22
  • possible duplicate of [Comet implementation for ASP.NET?](http://stackoverflow.com/questions/65673/comet-implementation-for-asp-net) – D'Arcy Rittich Oct 19 '11 at 18:30

3 Answers3

2

You could have each client poll the server, and at the server side keep the connection open without responding.

As soon there is a message detected at server side, this data is returned through the already open connection. On receipt, your client immediately issues a new request.

There's some complexity as you need to keep track server side which connections is associated with which session, and which should be responded upon to prevent timeouts.

I never actually did this but this should be the most resource efficient way.

2

Use long polling/server side push/comet:

http://en.wikipedia.org/wiki/Comet_(programming))

Also see: http://en.wikipedia.org/wiki/Push_technology

I think when you use long polling you'll also want your web server to provide some support in the form of non-blocking io for requests, so that you aren't holding a thread per connection.

Kevin
  • 24,871
  • 19
  • 102
  • 158
0

Nope. use queuing systems like RabiitMq or ActiveMQ. Check mongoDB too.

A queuing system will give u a publish - subscribe facilities.

Ravi Bhatt
  • 3,147
  • 19
  • 21