Using SignalR in MonoTouch and Mono for Android Apps

If you haven’t checked out SignalR yet, what are you waiting for? SignalR is a .NET library that makes it really easy to build realtime applications. The server can be hosted inside a web application, or even in a simple console app. On the client side, you can access the server from the browser through JavaScript, a console application, or even from mobile apps. Naturally my first instinct was to try and get a SignalR client working that can be used from MonoTouch and Mono for Android applications.

UPDATE 4/30/2013: Please see this more recent post for more up to date information: Updates On My SignalR Fork and Xamarin Support

Building the Client

I’d be lying if I said it took a ton of effort to get things working under Xamarin’s tools. In fact, it turned out to be a great case study in the power of the Xamarin platform. Since MonoTouch and Mono for Android expose a profile very similar to that of Windows Phone, I was able to use the Windows Phone client project as a starting point. By adding in some conditional compilation symbols in many of the same places Windows Phone had them, I quickly had a set of projects that compiled for MonoTouch and Mono for Android.

The one tricky part with iOS is that features of .NET that rely on JIT compilations will not work on a device, despite the fact that they will work in the simulator. Because of this, I added a handful of workarounds to the MonoTouch build to help work around these limitations. This included some usages of ConcurrentDictionary and some Json.NET features that relied on JIT. This was not necessary for the Android build, since Android allows for JIT compilation.

Besides the JIT workarounds, that’s really all it took to get the clients working. Think about that for a second: that means I was able to reuse all of the existing SignalR client code, and leverage it on entirely new platforms! If that’s not a great example of the power of using C# across all platforms, I’m not sure what is.

You can find my fork of SignalR on GitHub.

Sample Apps

Of course, a client library is not useful without apps that use it, so I went ahead and added a simple example app for both MonoTouch and Mono for Android to verify that everything is working as expected. You can find both of these in the samples folder in the repository. To keep things simple, both of these samples make use of the SignalR.Hosting.Self.Samples sample that comes as part of SignalR. This is a console application that listens on port 8081 for incoming connections. Make sure you fire that app up before running the samples to make sure things work correctly. Depending on how your network is set up, you may also want to update the IP addresses used by the samples to point to the correct place.


To simplify the UI creation, this sample also makes use of the MonoTouch.Dialog library. All of the work happens in the AppDelegate class in the app:

When the app starts up it will create a connection to the server, broadcasting a message saying it has connected once the connection is open. The use of SignalR here is extremely simple. To broadcast a message to all clients, the Send() method on the connection is used. Whenever a message is broadcast, the Received event is fired, letting us know a new message has arrived. It’s possible to send more complicated messages, but in this case we just send and receive simple strings.


For the layout, all the app needs is a text field, button, and list of messages received from the server:

With that in place, the DemoActivity class takes care of the rest of the work:

Outside of the Android-specific pieces, the code here is almost identical to that of the MonoTouch app, using the same Send() method and Received event on the connection.


My goal for my SignalR fork is to keep it up to date with any point releases from the main SignalR project. It tends to be a fast moving project so I won’t pretend to be able to keep up with the master branch all the time, but whenever there’s an actual new version I will try to keep my fork in line with that. As of right now, my fork is on SignalR version 0.6. SignalR has a ton of power and potential, so I hope some of you find it useful to be able to create clients in MonoTouch and Mono for Android apps!

Tags: , ,

12 Responses to “Using SignalR in MonoTouch and Mono for Android Apps”

  1. Great post and thanks for sharing it. I can seem me using this in my next monotouch app.

  2. Rob Gibbens

    This is awesome. I see so many possibilities for this.

  3. [...] Using SignalR in MonoTouch and Mono for Android Apps – Greg Shackles discusses getting up and running with SignalR on Android and iOS using Mono and MonoTouch, looking at building the client and consuming it in code. [...]

  4. [...] Using SignalR in MonoTouch and Mono for Android Apps Published Tuesday, July 31, 2012 6:50 PM by gOODiDEA Filed under: .NET, Performance, Node.js, WindowsPhone [...]

  5. Thanks for the demo apps too.

    Quite interesting ‘application’ of SignalR.

  6. What are the chances of your iOS changes getting into the main branch?

  7. When I originally spoke with David about it, he didn’t want to bring in MT/MD support into SignalR officially, which is why I continue to maintain this fork. That said, I believe there are plans to officially support it at some point, but I can’t really provide too much insight there.

  8. Hi Greg,

    your ports of SignalR are awesome, we have been using them for a cross platform mobile demo for a real-time auction and it works like a charm. It does not seem to work on Android 2.2 however, does your SignalR for MonoDroid only support Android 2.3 and higher?

  9. awesome post! thanks for sharing.

    I am a big fan of signalr.

  10. Hi Greg,

    I can’t find this sample anywhere in you forks.

    I have been trying to build a basic Mono for Android app using the chat example and have the Web and WinForms examples working fine.

    When I copy the code from my WinForms app into my mono app I am having a lot of trouble.

    The first issue I had with the latest .net client dll was all around 509 certificates. I could find any resolution so reverted to an older version of the dll.

    The problem then with the older version was anytime I went to call the invoke method it crashed out telling me there was no connection, even when I wrapped it in a new _connection.Start().ContinueWith(task => function.

    I could see that there was a connection because it was handling any incoming messages from the web clients.

    Do you have a recent version that supports the Android with Mono?


  11. Hi Tony,

    You’re right that the sample is no longer in my fork – a lot has actually changed since I wrote this post last year. The good news here is that the SignalR team recently took in my pull request and are working on official support for both Xamarin.iOS and Xamarin.Android, so my fork is no longer the place to go. As far as I know, they are planning to provide full support starting in SignalR 2.0. My changes are merged into their dev branch, so you can pull from that if you want to try things out in the meantime.

    Hope this helps!


  12. [...] Scott worked with Greg Shackles, a talented member of the Xamarin community who has a lot of experience with SignalR on Xamarin. After Greg and Scott collaborated on the demo, David Fowler of the SignalR project accepted [...]

Leave a Reply