views:

1169

answers:

6

Hello,

I have developed a chat web application which uses a SqlServer database for exchaning messages.

all clients poll every x seconds to check for new messages.

it is obvious that this approach consumes many resources, and I was wondering if there is a "cheaper" way of doing that.

I use the same approach for "presence": checking who is on.

thanks

Yaron

A: 

If you are using SQL Server 2005 you can look at Notification Services. Granted this would lock you into SQL 2005 as Notification Services was removed in SQL 2008 it was designed to allow the SQL Server to notify client applications of changes to the database.

If you want something a little more scalable, you can put a couple of bit flags on the Users record. When a message for the user comes in change the bit for new messages to true. When you read the messages change it to 0. Same for when people sign on and off. That way you are reading a very small field that has a damn good chance of already being in cache.

Do the workflow would be ready the bit. If it's 1 then go get the messages from the message table. If it's 0 do nothing.

mrdenny
Notification Services does not solve the problem of eliminating polling which is the focus of this problem. I guess Yaron is trying to find out a more effective and less resource-consuming way of updating the web-client of the chat messages/chatters information.
o.k.w
Hi, the focus of this question is not on polling. I know I can use "comet" (reverse-ajax) and avoid polling.My focus is: is it wise enough to use a database table for exchanging messages and maintaing the state of the participantsthanks :)Yarin
Yaron
Yes it is. Using a table is the only way to persist the messages through a server or service restart to ensure that you don't loose any messages.
mrdenny
+1  A: 

Without using a browser plugin/extension like flash or java applet, browser is essentially a one way communication tool. The request has to be initiated by the browser to fetch data. You cannot 'push' data to the browser.

Many web app using Ajax polling method to simulate a server 'push'. The trick is to balance the frequency/data size with the bandwidth and server resources.

I just did a simple observation for gmail. It does a HttpPost polling every 5 seconds. If there's no 'state' change, the response data size is only a few bytes (not including the http headers). Of course google have huge server resources and bandwidth, that's why I mention: finding a good balance.

That is "Improving user experience vs Server resource". You might need to come out with a creative way of polling strategy, instead of a straightforward polling every x seconds.

E.g. If no activity from party A, poll every 3 seconds. While party A is typing, poll every 5 seconds. This is just a illustraton, you can play around with the numbers, or come out with a more efficient one.

Lastly, the data exchange. The challenge is to find a way to pass minimum data sizes to convey the same info.

my 2 cents :)

o.k.w
I am polling by the state of the conversation. My question was aboubt - is there any other way to exchange messages besides using a table in the database."Sql Insert" statements are "expensive, and I am wonderin if I do need to use the database during a conversation, of is there any other, less expensive way. Thanks :)
Yaron
I see, guess I misunderstood the question.
o.k.w
A: 

In ASP.NET 4.0 you can use the Observer Pattern with JavaScript Objects and Arrays ie: AJAX JSON calls with jQuery and or PageMethods.

You are going to always have to hit the database to do analysis on whether there is any data to return or not. The trick will be on making those calls small and only return data when needed.

rick schott
A: 

There are two related solutions built-in to SQL Server 2005 and still available in SQL Server 2008:

1) Service Broker, which allows subscribers to post reads on queues (the RECEIVE command with WAIT..). In your case you would want to send your message through the database by using Service Broker Services fronting these Queues, which could then be picked up by the waiting clients. There's no polling, the waiting clients just get activated when a message is received.

2) Query Notifications, which allow a subscriber to define a Query, and the receive notifications when the dataset that would result from executing that query would change. Built on Service Broker, Query Notifications are somewhat easier to use, but may also be somewhat less efficient. (Not that Query Notifications and their siblings, Event Notifications are frequently mistaken for Notification Services (NS), which causes concern because NS is decommitted in 2008, however, Query & Event Notifications are still fully available and even enhanced in SQL Server 2008).

RBarryYoung
A: 

For something like a real-time chat app, I'd recommend a distributed cache with a SQL backing. I happen to like memcached with the Enyim .NET provider, so I'd do something like the following:

  1. User posts message
  2. System writes message to database
  3. System writes message to cache
  4. All users poll cache periodically for new messages

The database backing allows you to preload the cache in the event the cache is cleared or the application restarts, but the functional bits rely on in-memory cache, rather than polling the database.

Chris
Thanks, I will check it out.My reason tells me that if it's a one-to-one conversation, and I am pollling only for new messages, then I can't use the cache (if it was a group chat it was probably better), but I will check it out.Thanks :)
Yaron
You could still use a cache. Ideally you'd keep an store an index object per user that contains a cache key for each message from that user along with the intended target. Your code would check that index object for new messages targeting the polling user's ID (or group).
Chris
A: 

Hi,

I've recently published an opensource chatting application over at CodePlex. It takes inspiration from Google Talk as well as Facebook chat.

It uses XML to store messages and chat sessions etc. so it can be easily integrated into any ASP.NET web application

Check it out @ http://aspnetjquerychat.codeplex.com/

Ali Kazmi
Looks good, I'm checking it out. thanks
Yaron
I see you use IHttpAsyncHandler for long polling. I use it too, and I have a problem with the status-bar in FireFox. It constantly shows "Waiting for <domainname>..."Do you know any solution?thanks again
Yaron