NodeJS edged with C#

NodeJS edged with C#

Hi everyone, it's been a while I didn't post on my blog mainly because I didn't had the time for mainly because I was coding some new stuff and working on a project of mine you'll know about really soon.

But enough of my life, today I wanted to show you some really cool piece of code, called edgejs ! There is some time that I'm seeing Microsoft making real efforts to participate to opensource community projects (some would say for their personal interests but never mind), and I personally think it is great to have major IT company to make that effort. Software development is about sharing knowledge and making great stuff that anybody can have access to.

That's why I've been making some experiments with really interesting projects like typescript, nancyfx or mono. These projects are pushed by opensource community and take advantage of Microsoft solid engineering projects to improve them. It's an interesting step toward open community or full disclosure as called in the IT security world.

Edge.JS


But enough of these thoughts for now, today I want to introduce a really interesting project created by  Tomasz Janczuk and called Edge.js and defined as :
"The Edge.js project allows you to use .NET Framework inside of a Node.js application."
This magical project allows you to call C# methods from NodeJS or NodeJS functions from a C# project. Why the hell would you want to do that you would say ? To find why it is very interesting, you have to go to the definition of both language/framework. NodeJS work in an asynchronous single threaded environment, but CLR allows you to work in a multi threaded environment. You have the best of both worlds ! For web development native asynchronous environment are really useful but for heavy computation you would rather prefer to use all available cores or even GPU capabilities !

Proof of Concept


So how it works ? You should have a look at the documentation explaining it very well and/or Tomasz blog for some extra stuff. In this post I will only explain how to create your mono DLL and call it from your NodeJS router.

First of all we'll create our mono library project to generate the DLL. You can imagine a prime number computation using multi-threaded algorithm but to make it simple we'll only create a function that takes an integer and sum it with himself :

using System;
using System.Threading.Tasks;

namespace PrimeNumber
{
    public class Startup
    {
        public async Task<object> Invoke(object input)
        {
            int v = (int)input;
            return Helper.Add (v);
        }
    }

    static class Helper
    {
        public static int Add (int v)
        {
            return v + v;
        }
    }
}


As you can see we have a Helper::Add method in charge of this. The other method should, by convention, be named Startup::Invoke. We define it as async so the call will be non-blocking in NodeJS side. Actually we also need to use await operator to have a really asynchronous call but because this is an introduction we do not integrate it but you can refer to this post for more details.

We compile our DLL to PrimeNumber.dll and copy it to our NodeJS project in assets directory and see what's next in node side.

On NodeJS side we have a server.js file that looks like this :

var edge = require('edge');

var clrMethod = edge.func('assets/PrimeNumber.dll');

clrMethod(123function(error, result){
  if(error) throw error;
  console.log(result);
});

As you can see it is pretty easy to use. By default the method Startup::Invoke is called in the same namespace as filename PrimeNumber but you can change almost all these options (see edge documentation)

Conclusion


As you can see this is pretty impressive. The project is still in development but open a lot of possibilities for web developers as us. I hope that this quick tour helped you.

See you, Flyers.

Most seen