Monday, July 26, 2010

Silverlight and .NET clients for the Laharsub publish/subscribe service for web clients

The just released 2010.07.25 stable build of the open source Laharsub project adds Silverlight 4 and .NET 4.0 clients. The current release contains:

  1. A Laharsub publish/subscribe server based on HTTP long polling protocol. The server is implemented using a .NET 4.0 WCF HTTP service and exposes REST APIs optimized for web clients.
  2. A .NET 4.0 client.
  3. A Silverlight 4 client.
  4. A jQuery AJAX-based JavaScript client.

I am looking forward to your comments and suggestions. You can read more about the open source Laharsub project at http://laharsub.codeplex.com.

19 comments:

  1. Hi Tomasz. I took one of your samples modified it a little bit, and now I can reproduce bug, that I experience in my environment. Bug(problem): if client (slow internet, or what ever reason) keeps reconnecting to the pollingduplex service, at some point ServiceHost will shut down, disconnect all existing clients, and won't accept any new connections. Sample will work perfectly if it hosted in IIS7, but will have that bug, if it is selfhosted. Any ideas? Thank you, Oleg.

    ReplyDelete
  2. Oleg, I am not sure which sample you are referring to. Laharsub is not meant to be IIS hosted, so it must be a different one?

    ReplyDelete
  3. Hi Tomasz,
    link is http://tomasz.janczuk.org/2009/07/pubsub-sample-using-http-polling-duplex.html. I updated it to use polling duplex 4 and run as selfhosted app, as like I said before I got all this problems. THis is case number, I submited that bug to microsoft(if you want to look at) 110081680740542.
    Thank you, Oleg.

    ReplyDelete
  4. Alessandro FedericiAugust 20, 2010 at 4:59 AM

    Hi Tomasz,
    thank you very much for this! I am VERY interested in using this technology. I downloaded the latest build from codeplex but I am running in a silly problem... Your service listens on port 80 and your instructions seem to assume IIS is also supposed to be listening on that port. The two clash. I changed the IIS setting to have it listen on port 81. At this point I can get to jquery.chat.html but when I click on the "Create new chatroom" I get a "The page cannot be displayed" error. I will try seeing if there's something in your config files to get around this but it would be great if you can provide some new instructions on the codeplex site (also, current docs refer to chat.html which does not exist anymore).

    Thanks,
    Alessandro Federici

    ReplyDelete
  5. Alessandro, you should be able to host both the Laharsub service and a web site in IIS on the same port (including 80) as long as the base address of the Laharsub service is different from the web site location. For example, you can host Laharsub at /ps/memory (the default in Laharsub.exe.config) and a web application at /webapp, both on port 80. However, if you attempt to create your web application at /ps as well, the two will in fact clash. Thank you for pointing out the issue with documentation, I will get it fixed.

    ReplyDelete
  6. Alessandro FedericiAugust 20, 2010 at 5:21 PM

    Thanks for the prompt response... I feel kind of silly as that was the very first thing I tried (at 7am ). Maybe it has something to do with the fact I am running IIS on XP? Anyhow, I'll figure a way and retry tomorrow. Since I got the html/js files, I can see what goes on. I will let you know what I find. I am so excited about this project: it's *precisely* what I was about to start writing and I am glad to see there's interest on your side to have this technology done.

    ReplyDelete
  7. Alessandro FedericiAugust 20, 2010 at 5:23 PM

    Forgot to mention, right after I told you about IIS on XP: if I start your service when IIS is running, the service doesn't start. If I stop IIS, start your service, then try to run IIS, that doesn't work. Both apps claim (obviously) something else is listening on that port.

    ReplyDelete
  8. Alessandro, IIS running on Windows XP does not support port sharing. You can read about it at the bottom of http://msdn.microsoft.com/en-us/library/ms733768.aspx. Given that client operating systems (like XP or Vista) have limits on the number of concurrent incoming HTTP requests, they are not good platforms for hosting HTTP services, in particular based on HTTP long polling. Consider using a proper server OS SKU to host Laharsub and IIS.

    ReplyDelete
  9. Alessandro FedericiAugust 20, 2010 at 7:25 PM

    I figured was something like that, I am glad I mentioned XP :)

    Dzienkuje bardzo

    PS My wife is Polish and we're guessing you are too. If not, add it to my "silly" list

    ReplyDelete
  10. Alessandro FedericiAugust 23, 2010 at 8:44 AM

    Tomek, I installed the thing on Windows 7 ultimate where IIS with port sharing is available but I ran into security issues even if I ran the service (/console) as administrator. I don't recall the error message now (I was trying this at home), but it was suggesting about "elevating" the current user to administrative level or something (although the user was indeed an administrator). I think I am going to have to pass on trying some more right now (our development environments are on XP for the time being and I simply don't have the time to build a Win7 or server/os version of them).

    Let me make a suggestion: if the service was be able to provide a clientaccesspolicy.xml to a requesting client *and* the clients (both SL and jquery) offered a way to use a different server than those they were ran from, this whole issue would go away and we could try this on any OS (for development/testing purposes as least). I think, in general, having the server be on a different IP than the web server is something people will need in real life and being able to rely on different web servers (old IIS on say windows 2000, 2003, Apache, or, in general, something that does not take advantage of kernel-mode HTTP stack) would also be a welcome addition (although not as important as the 1st).

    ReplyDelete
  11. Alessandro,

    first of all, thanks for all the feedback.

    I in the Windows 7 (and Windows Server 2008)security model it is not sufficient to be logged in as an administrator, one must also explicitly run a particular process with elevated privileges. One way to run Laharsub.exe that way is to open a cmd.exe window by right-clicking on the shortcut and choosing "Run as administrator", and then starting Laharsub.exe /console from that command window.

    The Laharsub.exe could provide a way to serve up clientaccesspolicy.xml file. However, serving this file would have to be mutually exclusive with IIS running on port 80 on the same machine (even on OSes that support it). Given that, I think most people would prefer to just drop the clientaccesspolicy.xml file to the root of the IIS default web site and preserve the ability for Laharsub and IIS to co-exist on the box. However, I will think of adding a switch to Laharsub.exe to enable hosting of clientaccesspolicy.xml when IIS running side-by-side is not a consideration.

    A Laharsub client can be pointed to a Laharsub server running at a host other the the host of the IIS web application. To do this, you need to instantiate the PubsubClient with a constructor that specifies the base address of the Laharsub server (e.g. new PubsubClient("http://my.server.com/ps/memory")).

    ReplyDelete
  12. Alessandro FedericiAugust 24, 2010 at 8:01 AM

    YAY! Made it work. Thanks for the tips (I couldn't not try it again after your last post).

    As far as the clientaccesspolicy.xml topic, I agree that if laharsub runs side-by-side, IIS is the best candidate to provide that file. But if they are not running side-by-side and there's no IIS on the machine where the service would be running (very common scenario IMHO), it could be handy. Anyhow, here's some notes:

    I ran 4 chat js clients at the same time and broadcasting of messages was pretty much instantaneous.
    Great work.

    The SL client only seems to perform tests, so I didn't do much with it (and, btw, all tests passed).

    Here is a critical issue I found:

    -Somehow, if I leave the 4 js clients idle for a bit (went to smoke a cigarette after sending some messages to see it working) , the browsers started to produce errors and stopped working. The error you get is the following:

    ------------------------------------------------------------------
    Webpage error details

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
    Timestamp: Tue, 24 Aug 2010 14:30:58 UTC


    Message: Not enough storage is available to complete this operation.

    Line: 127
    Char: 287
    Code: 0
    URI: http://code.jquery.com/jquery-1.4.2.min.js
    ------------------------------------------------------------------

    I could reproduce this consistently 3 times in a row. The very last time this happened I checked the Task Manager and I could see how each of the IE processes had something around 1Gb (yes, it's not a typo) of memory in use (which is consistent with the error message above). Laharsub.exe is stable at 2772kb and IIS is showing no issues either.

    Questions, in general:

    -what are your plans (at least on the SL front), for disconnected scenarios? What we need here is a pub/sub system that can queue messages on the client side in case of loss of connectivity to the LAN/WAN and can survive restarts. Is there any plan to queue messages in, say, the isolated storage and broadcast them when connectivity is back?

    -I see that, for now, messages are stored in memory: what are the plans for server farms and/or server restarts? Is there, say, an MSMQ or DB storage coming up?

    ReplyDelete
  13. Alessandro,

    I am glad it worked for you.

    Which sample were you using when the error occurred? the jquery.chat.html or the jquery.test.html?

    I have no plans to support durability or disconnected scenarios at the moment. When you say "queue messages in isolated storage", I assume you mean the messages to be published by the client? I believe such functionality could be built on top of the PubsubClient in Silverlight.

    One of the goals of Laharsub is to experiment with a variety of backend technologies, and I will be looking at durable storage going forward. Currently I have a prototype backend implementation that is based on MSSQL. However, its performance and scalability in nowhere near where I want it to be before sharing publicly.

    ReplyDelete
  14. Alessandro FedericiAugust 24, 2010 at 12:34 PM

    I was using jquery.chat.html

    Too bad for the disconnected scenarios not supported :( It's sort of critical for our apps.

    And yes, I meant the messages published by the client, although that was for the purpose of re-sending when a connection is reestablished or the app-restarted (after not being able to push messages to the server and working offline).

    ReplyDelete
  15. Hi Tomasz,
    After I learn more things about duplex communication with your good post
    and video tutorial, now I'm in the middle of my sl chat app.
    In my SL chat app when I handle OperationContext.Current.Channel.Faulted
    and OperationContext.Current.Channel.Closed
    Events of my duplex service when a user closes his browser these events
    not called, why?
    after that i use a timer and check client states in a special interval
    but when I do this,
    I found when the clients close their browser the states of client's
    never equals closed or faulted, what is wrong here?
    and In the client app I write a singleton factory class for share the
    WCF client service in the all of my app.
    So can be the problem for this?

    ReplyDelete
  16. Mahdi, despite the HTTP polling duplex implementation manifests itself as a sessionful channel on the server side, the channel does not offer session specific behaviors in terms of lifetime events. This is due to a connection-less nature of the underlying protocol. In particular, the Faulted and Closed events are not going to be immediately raised when the client disappears (e.g. closes the browser window). Detecting that the client has gone away and faulting the channel is based on timeouts, in particular PollHeartbeatTimeout and InactivityTimeout described at http://blogs.msdn.com/b/silverlightws/archive/2010/07/16/pollingduplex-multiple-mode-timeouts-demystified.aspx. In addition, messages the server attempts to send back to the client will time out after the SendTimeout is reached.

    ReplyDelete
  17. Thanks Tomasz
    I read that good article about timouts,
    after that I change my inactivity & serverPoll Timouts until low the PollHeartbeatTimeout,
    at this time this was better than later in showing
    states of client but my server faulted very soon.

    Can I do removing clients without checking their
    state or using closed & faulted events?
    Can you show me a sample that works well in this subject?

    ReplyDelete
  18. Mahdi, could you post any further questions regarding Silverlight's polling duplex to a more appropriate blog post, e.g. http://tomasz.janczuk.org/2009/07/pubsub-sample-using-http-polling-duplex.html? This post is related to the Laharsub project.

    I don't think there is any better way of detecting a client has disconnected that hooking up the Closed/Faulted events. These events will also be raised as a result of a failed attempt to send a message to the client that has disconnected.

    ReplyDelete
  19. Excuse me, when I want to post on that page with firefox, I get this error(Service unavailable),
    but I don't get that with IE.

    Now I post my next question on that page.

    Thanks

    ReplyDelete

My Photo
My name is Tomasz Janczuk. I am currently working on my own venture - Mobile Chapters (http://mobilechapters.com). Formerly at Microsoft (12 years), focusing on node.js, JavaScript, Windows Azure, and .NET Framework.