SignalR Self Hosting With .NET Client

If you have not heard of SignalR, you can think of it as a kind of RPC (remote procedure call), but it is much more advanced than that, where clients may be called by server code, and clients may call server code.

Here is what the SignalR web site has to say about itself:

ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It’s the ability to have your server-side code push content to the connected clients as it happens, in real-time.

You may have heard of WebSockets, a new HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it’s available, and gracefully fallback to other techniques and technologies when it isn’t, while your application code stays the same.

At the time SignalR was very novel (even though that article was only written a year ago) and the only option at that time was a JavaScript client with a ASP .NET hosted Hub. Things have obviously come on leaps and bounds since then. Now there are all sorts of options for using SignalR, here are some options available to you now

.NET client (OWIN) client (OWIN was new to me, here is what it means : Open Web Interface for .NET which you can read about more here : OWIN)

Azure client

Javascript client

Redis client

There is also the possibility to self host the Hub which was of particular interest to me for a new side project I wanted to work on. To this end I decided to give the following scenario a go:

A simple echo hub that would receive messages as strings, which would be reversed and sent to ALL connected clients. Now the clients could be .NET or JavaScript or any of the other supported hubs. For my requirements .NET was all I cared about.

A .NET client that would send messages to the server (via the console standard input), and would print out any messages from the server (via the console standard output)

The reason I decided to give this a go is that I could not find a single source which had a running demo example of self hosting, and using .NET as a client. So I hope this helps someone.

Share this:

Like this:

Related

16 thoughts on “SignalR Self Hosting With .NET Client”

Thanks for this article. SignalR is a great project, and being able to self-host it opens very interesting possibilities.

There’s something I don’t understand in your sample project, though; what is the role of the RawConnection class? I put breakpoints in the OnConnected and OnReceived methods, but they’re never hit, and it works exactly the same if you remove the class completely.

Great information for my similar ongoing project. Can you guide me how can I use/compile this demo in .net framework 3.5? Libs included in demo project is for .net 4
btw have a Thanks for useful information 🙂

No unfortunately I don’t think I can get it to work with .NET 3.5. In fact I am struggling to get a .NET4.0 version working for myself. Seems once you open it in VS2012 and build things one of the mscorlib.dll things have moved. Nightmare

Its shiny its new man. No seriously yeah you could do this with WCF for suer, bit more work though as you need to mantain callback and client list yourself (make it thread safe etc etc), and that would only work with .NET. Where as SignalR hub would work with many different types of client

A response I was expecting. Like youR style! SignalR makes COMET or reverse ajax much easier. What I like is the fallback when sockets are not about. For those in the financial sector this is common place as IE6/7 is still rampant.

If you are using ASP MVC there are loads of examples. If you want to NOT use ASP MVC, you will have to self host the Signalr hub and use the .ENt client, code would be very simliar to ASP MVC example though, except the host part