none
Chatroom functionality with WCF, duplex callbacks vs polling? RRS feed

  • Question

  •    

    I am using WCF and I am putting a chatroom facility in my C# program. So I need to be able to send information from the server to the clients for two events -

    • When a user connects/disconnects I update the list of connected users and send that back to all clients for display in a TextBlock
    • When a user posts a message, I need the server to send that message out to all clients

    So I am looking for advice on the best way of implementing this. I was going to use netTcpBinding for duplex callbacks to clients but then I ran into some issues regarding not being able to call back the client if the connection is closed. I need to use percall instances for scalibility. I was advised in this thread that I shouldnt leave connections open as it would 'significantly limit scalibity' - WCF duplex callbacks, how do I send a message to all clients?

    However I had a look through the book Programming WCF Services and the author seems to state that this is not an issue because 'In between calls, the client holds a reference on a proxy that doesn’t have an actual object at the end of the wire. This means that you can dispose of the expensive resources the service instance occupies long before the client closes the proxy'

    1. So which is correct, is it fine to keep proxies open on clients?
    2. But even if that is fine it leads to another issue. If the service instances are destroyed between call, how can they do duplex callbacks to update the clients? Regarding percall instances, the author of Programming WCF Services says 'Because the object will be discarded once the method returns, you should not spin off background threads or dispatch asynchronous calls back into the instance'
    3. Would I be better off having clients poll the service for updates? I would have imagined that this is much more inefficient than duplex callbacks, clients could end up polling the service 50+ times as often as using a duplex callback. But maybe there is no other way? Would this be scalable? I envisage several hundred concurrent users.
    Monday, December 19, 2011 10:28 PM

Answers

  • Hello, I don't know which book you're referring to. But I think "Because the object will be discarded once the method returns" means if your service method uses some resources internally, they will be disposed as long as you handle them properly. For example, your service implementation may use an object that implements IDisposable. If you invoke Dispose before returning the response, you won't leak any memory. This is service implementation detail which is unknown to the client.

    In most cases, it is desirable to leave a TCP connection open for a certain amount of time. This should not cause scalability issues. Just do not leave idle connection open for too long (say, several hours). By default, WCF will close idle connections after a while. This is controled by timeout settings on the binding. Once a connection timeout, you must establish a new connection (create a new client proxy instance for example).

    Polling is also a good solution for text based chatting. Text based chatting usually do not need to be real time. Several seconds delay is acceptable. Actually today there're a lot of web based chat applications that uses http polling (such as the AJAX version of Windows Live Messenger, Facebook chat, and GMail chat).


    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.
    • Marked as answer by Yi-Lun Luo Monday, December 26, 2011 9:30 AM
    Wednesday, December 21, 2011 2:04 AM

All replies

  • Take a look at this.

    This can answer many of your questions .

    http://www.codeproject.com/KB/WCF/WCFWPFChat.aspx

    Thanks.


    Get Coded ..............
    Monday, December 19, 2011 10:38 PM
  • That code in that project is helpful to me mate. But it doesn't really answer my specific questions because it uses persession instances so a service object will exist for every connected client. My questions are about percall instances where the service objects are destroyed between calls.
    Tuesday, December 20, 2011 12:28 AM
  • Hello, I don't know which book you're referring to. But I think "Because the object will be discarded once the method returns" means if your service method uses some resources internally, they will be disposed as long as you handle them properly. For example, your service implementation may use an object that implements IDisposable. If you invoke Dispose before returning the response, you won't leak any memory. This is service implementation detail which is unknown to the client.

    In most cases, it is desirable to leave a TCP connection open for a certain amount of time. This should not cause scalability issues. Just do not leave idle connection open for too long (say, several hours). By default, WCF will close idle connections after a while. This is controled by timeout settings on the binding. Once a connection timeout, you must establish a new connection (create a new client proxy instance for example).

    Polling is also a good solution for text based chatting. Text based chatting usually do not need to be real time. Several seconds delay is acceptable. Actually today there're a lot of web based chat applications that uses http polling (such as the AJAX version of Windows Live Messenger, Facebook chat, and GMail chat).


    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.
    • Marked as answer by Yi-Lun Luo Monday, December 26, 2011 9:30 AM
    Wednesday, December 21, 2011 2:04 AM