Sunday, July 6, 2014

Dart/Javascript Interop - Creating a SignalR client wrapper in Dart

I've recently been exploring the idea of using Dart for the client side of my next web project as I think the language lends itself to a more maintainable code base at larger sizes copared to JavaScript. However, due to the large number of frameworks JavaScript already has at it's disposal, it would be silly to not try to leverage those frameworks. One in particular, is the SignalR JS client. I really like SignalR as a technology, it's easy to use and provides great real-time support to your .NET web application. However, the idea of writing my own Dart client from scratch to support two way communication with a SignalR server seems like it would be quite a large effort. So I decided to see how hard would it be to just create a Dart wrapper around the existing JS library.

Example server code

In this example, we should first go over what is on the server. I've built my client separate from the server so I needed to enable CORS support. Strangely, this support in SignalR is in an entirely separate NuGet package, which in my mind, seems a little odd. Make sure you have the installed the SignalR server and CORS NuGet package onto your server project.

Install-Package Microsoft.AspNet.SignalR
Install-Package Microsoft.Owin.Cors

To setup SignalR, you need a Startup class with a [assembly:..] reference to it.

You will also need to register a Hub.


Example in JavaScript

Now, the JS code to access this hub (without JS code gen) is pretty simple and looks like the following snippet.


This is assuming your page is hosted on the same server, SignalR defaults urls, something we are not going to do for our Dart client.

Example in Dart

I wrote the wrapper to try to keep the implementation code as similar to the JavaScript equivalent as possible. 


JavaScript Interop

The enable the code above, I wrote a small Dart package to wraps the connection and proxy objects exposed in the JavaScript library that SignalR provides. So this code still requires that the SignalR JavaScript client is included in the page as well as jQuery.

Wrapping an existing JS library is pretty straight forward, though debugging can be difficult at times, I think the Dart team has done a pretty darn good job with it. Here is all the code that enables the HubConnection and HubProxy classes.




Obviously this doesn't wrap everything in the JS lib, but enough to get two-way communication going for a lot of situations and as you can see above, is pretty straight forward.

Conclusion

Although that Google hasn't announced when the DartVM will be embedded in Chrome, it probably isn't far off. It has been said that JavaScript is the assembly language of the web, and for asm.js, I think that analogy is pretty damn close. After writing some JS interop, it really reminded me more of a less painful COM interop from .NET. All in all, I think people are going to want to leverage this feature a more going into the future. I still think Dart is moving in the right direction, more so than TypeScript and others. And as Dart usage continues to grow into Servers, it's going to make more sense to use it more for clients as the DartVM grows in support.