WCF WebSockets: First Glance

I finally got around to playing with the first drop of WebSockets support for WCF. I’m pretty familiar with WebSockets as I’ve been using Node.js to play around with them for quite a while now. The server API in Node.js is wonderful as it couldn’t be simpler – to demonstrate this, take a look at how we’d create a basic echo server.

var ws = require("websocket-server");

var server = ws.createServer();

server.addListener("connection", function(connection){
  connection.addListener("message", function(msg){


When I heard that Microsoft was planning on integrating WebSockets into WCF my first thoughts weren’t all that positive. Now I know it’s not quite the case since .NET 4, but WCF has had a reputation for rather large complex API’s and heaps of XML. I was dreading what a WCF take on the above would look like. Turns out, perhaps I should be a little more optimistic.

class Program
    static void Main(string[] args)
        var host = new WebSocketsHost(new Uri("ws://localhost:4502/echo"));

public class EchoService : WebSocketsService
    public override void OnMessage(JsonValue jsonValue)

That’s really the kind of simplicity I was really hoping to see. Now the interesting question is how it’s going to look when dealing with multiple clients. Node kind of has an advantage here as it’s entirely single threaded (yeah, I said advantage), but with .NET’s recent concurrent collections and a rather nice API for dealing with events (Rx) I’m feeling pretty hopeful.


12 thoughts on “WCF WebSockets: First Glance

  1. That is really quite cool. Between that and the HTTP/REST stuff Glenn Block has been working on for WCF it seems like WCF is starting to move in new and better directions. Still, they should change the name. Too many bad memories for some of us ;-).

    1. Thanks for pointing this out. The prototype has now moved here.

      They’ve also just dropped full WebSockets support in .NET 4.5. I think currently the only way to mess with it is to pull down Win8 dev preview and get VS11 through MSDN. It looks like WebSockets support on the server will require IIS8 if you want to sit alongside a web site. Of course, I could be completely wrong.

  2. Hello,

    I´ve tried to get a wcf websocket server working just by echoing incoming message. But i can´t seem to get it to work. I have a test html-document with some simple javascript to try the connection, but i dont get any response from the server.

    I´ve tried to set it up exactly the same way you have but no luck…. when i check the response header i get from the server it says

    HTTP 1.1 0 or HTTP 1.1 undefined undefined

    Any clues what the problem might be?

  3. I’m having exactly the same problem. My server doesnt respond to the initial connection upgrade GET and sends an http 504 response. I’ve scoured the net looking for answers to this and I cant find any. I’m pulling my hair out. A self-hosted console app works with a JS client but as soon as I move to IIS 8 everything grinds to a halt. I dont know where to start debugging this as the error returned by the JS websocket provides no clues as to why this is happening. IIS logging is equally un-useful.

  4. Hey! I could have sworn I’ve been to this blog before but after
    reading through some of the post I realized it’s new
    to me. Anyways, I’m definitely delighted I found it and I’ll be bookmarking and checking back

  5. Usually I do not learn article on blogs, however
    I wish to say that this write-up very pressured me to try
    and do it! Your writing style has been surprised me.
    Thank you, quite nice article.

  6. I have noticed you don’t monetize your page, don’t waste your traffic, you can earn extra bucks
    every month because you’ve got hi quality content.

    If you want to know how to make extra $$$, search for: best adsense alternative
    Wrastain’s tools

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s