1

I'm creating a very simple chat application. It has an ASP.NET web page as front-end and a WCF service as back-end for storing the messages to a database.

Everything works great except one thing; when Browser A enters a chat message I want Browser B to see the message as soon as possible (yeah, I know, that's the purpose of a chat).

What I've done so far is to setup a trigger within the UpdatePanel, like this:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="chatTimer" EventName="Tick" />
</Triggers>

which uses a timer:

<asp:Timer ID="chatTimer" runat="server" OnTick="chatTimer_Tick" Interval="1000" />

Is this the best approach or is there a better, yet simple, way to accomplish updating of messages. One drawback with this solution is that the textbox used to enter chat messages loses focus every time the Tick event runs.

Any piece of feedback or advice regarding updating of messages is appreciated.

Thank you!

Daniel
  • 429
  • 2
  • 9
  • 20
  • Have you considered doing this in silverlight? You might be able to leverage sockets to push updates to clients, rather than pull updates every second. – Juliet Jan 02 '11 at 23:46
  • At the moment, no I haven't. At the same time, your proposal sounds really nice. This is actually what I would like to do. – Daniel Jan 03 '11 at 05:05

3 Answers3

2

As the HTTP protocol doesn't support server push, you have to poll the server for changes. There are different ways of doing that, but they are all similar to what you are doing.

You can make the update of your update panel optional by setting UpdateMode="Conditional", that way it won't update (and lose focus) at every timer tick. It only updates when you have called the Update method on the server side to trigger it.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thank you, kind of answer I was looking for. That is, server push not available in ASP.NET. – Daniel Jan 03 '11 at 05:15
  • @Daniel: Yes, but it's the HTTP protocol that is the limitations, not the ASP.NET framework. :) – Guffa Jan 03 '11 at 06:05
2

Comet is a technique used for this kind of scenario. Although I wouldn't say it is simple.

Basically the client would hold a long lived connection open to the server, then the server can push data to the browser.

This question is related: Comet implementation for ASP.NET?

Community
  • 1
  • 1
Iain
  • 10,814
  • 3
  • 36
  • 31
1

If you're using WCF you could implement a CallbackContract that the server calls whenever a message from other clients arrive.

as-cii
  • 12,819
  • 4
  • 41
  • 43
  • But the callback contract would be fairly hard to implement on the client side, wouldn't it? – Xavier Poinas Jan 02 '11 at 23:49
  • You can only do that with NetTcpBinding and NetNamedPipeBinding. I don't think a browser will support that. He is not using WCF to communicate with the client (browser). – Iain Jan 03 '11 at 00:00
  • This is not true. WSDualHttpBinding supports that (http://msdn.microsoft.com/en-us/library/system.servicemodel.wsdualhttpbinding.aspx). I know he isn't using WCF to communicate with the clients but I've suggested him to do it. – as-cii Jan 03 '11 at 00:05
  • I'll take a look at this as well. – Daniel Jan 03 '11 at 05:21
  • Your right, there is also WSDualHttpBinding. If my understanding is correct, it sets up a connection on the client side that the server can connect to. I'm not sure how you can do that in the browser, and you also have problems with firewalls. – Iain Jan 03 '11 at 08:10