Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Samuel Jack, the founder of TruthVine, a start-up that takes the hassle out of sharing audio sermons on church websites. He is also a freelance .Net consultant, working on projects as diverse as Flow, music making software for DJs, to RavenDb, the NoSQL database. He blogs at, and you can follow him at @samuel_d_jack.

SignalR is a Microsoft .Net library for real-time communications: sending events back and forth between the client and the server. The Rx Framework (Reactive Extensions) is a Microsoft .Net library for processing event streams. Naturally, they work great when you put them together. Here’s a little example to get you thinking.

SignalR’s favored modus operandi is a Remote Procedure Call style. That’s what its Hub class provides. Your server invokes a method on a Hub, and the same method gets invoked on all connected clients. The clients invoke a method on their Hub proxy, and that same method gets invoked on the server.

But SignalR also allows you to communicate simply by sending events, which fits neatly with how the Rx Framework thinks about the world. To work with events in SignalR you need to be using its PersistentConnection class. PersistentConnection functions like the Controller class in ASP.Net MVC. You derive your own class from it, and then you map that to an Http route. Whenever a client interacts with the connection, your class gets instantiated to serve the request. SignalR will keep a communication channel open with every client that connects, so you can broadcast messages to them.

Broadcasting events from the server

Suppose you want a SensorEventsConnection that will broadcast readings taken from sensors to all connected clients. Let’s create a class:

Now let’s make this connection listen at /sensor/events by modifying Startup.Configuration:

You broadcast events through the connection by getting hold of its IPersistentConnectionContext:

That snippet of Rx code produces a dummy sensor event from one of three sensors every 50 milliseconds, then broadcasts it to all clients.

Processing events with Rx

Suppose you wanted to know whenever two or more sensors are reporting a reading higher than 0.75 at the same time. Here’s a little console app to do just that (to make this work you need the SignalR .Net Client NuGet package, the Rx NuGet package, and also the Rxx library, which I had to compile myself because its NuGet package isn’t up to date):

The key thing to note here is connection.AsObservable<SensorEvent>(). This takes the events being sent by the server, and makes them available as an IObservable stream so that they can be processed by the Rx operators. The GroupBy operator splits the combined stream of events into a stream for each sensor, then the CombineLatest method listens to all those streams, and whenever any one of them produces an event (a new reading from a sensor), it produces an event containing the latest readings from all of the sensors.


As you can see, SignalR gives an elegant method of streaming events to clients, and the Rx framework gives an elegant method of processing them.

Download the code from GitHub to see this in action.

Take a look at my Getting Started with SignalR and Reporting Server-Side Progress to Web Pages with SignalR posts as well.

Look below for some great SignalR resources from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

SignalR: Real-time Application Development is a hands-on, step-by-step guide that will take you from the basics of SignalR to more advanced techniques, such as making your application cloud ready and making it scalable. If you want your applications to be enterprise grade then SignalR, together with this book, is the answer.
ASP.NET MVC 4 Mobile App Development helps you to develop next generation applications, while guiding you to deal with the constraints the mobile web places on application development. By the end of the book, you will be well versed with all the aspects of mobile app development. This book also contains a good section on SignalR.
Real World Windows 8 Development is for .NET developers wanting to utilize their existing skills in XAML and C# towards building a Windows 8 application. On the fence about how your C# and .NET skills apply in the new WinRT world? Have a dream application idea that you slowly want to build up? This book is for you. Be sure to also look at the Signal R sections.

Tags: ASP.NET MVC, Microsoft, open source, Remote Procedure Call, Rx Framework, SignalR,

Comments are closed.