Edge.js bridges the gap between Node.js and .NET

Edge.js connects Node.js and .NET, and allows you to use the best features of each platform. Find out what Tony Patton finds most intriguing about this solution.

Image: arhon

As a .NET developer, I always approached situations with options from my .NET toolbox. This changed in the past couple of years, as the delineation between platforms and technologies has blurred.

For example, a number of open source options use .NET. In addition, the explosion of JavaScript has changed the landscape of web development; it is no longer relegated to the client, as Node.js is now a standard for web application development. The problem with such a solution is it negates a large percentage of my programming know-how because solutions are either based in .NET or Node.js.

A drum roll as Edge.js enters with the promise to combine the two technologies, so you can use what is best for the task at hand.

Connecting nodes

As the documentation states, an edge connects two nodes; in this case, Edge.js connects Node.js and .NET. With the current release, you can call .NET code from within your Node.js application or call Node.js from .NET -- this allows you to use the best features of each platform.

Node.js is great for real-time, high throughput web applications because everything is asynchronous (chat, application monitoring, data streaming, etc.) and single threaded, whereas more CPU-intensive tasks are best handled elsewhere like .NET. Edge.js requires at least .NET 4.5 for the async support. Configuring your environment to utilize Edge.js is a straightforward endeavor; we'll begin with calling .NET code from Node.js.

Hooking into .NET from Node.js

On the JavaScript side, the only thing needed is the JavaScript library. It is easily installed via npm (Node Package Manager) with one easy command:

npm install edge

If you're using Visual Studio to build your Node.js application (via Node.js Tools for Visual Studio plugin), you can use the npm Package Manager, which can be accessed by right-clicking your solution (Figure A). Once selected, the npm Package Manager interface is presented (Figure B). The search box at the top allows you to enter the package name (in this case, edge) and click the Install button to add it to the solution. The dropdown box between the search field and the Install button allows you to specify global or local installation. Once a package is installed, it will appear under the npm node within the solution (Figure C).

Figure A

Accessing npm Package Manager in a Visual Studio solution.

Figure B

The npm Package Manager interface allows you to manage packages.

Figure C

Once installed via npm, the package appears in the solution.

With the package installed, the Edge.js features can be used within the code. Figure C shows sample code taken from the Edge.js documentation -- it's a simple application that displays text on the web page once it is called. The key parts of the code are the second line where the Edge.js package is referenced with the require statement. With that in place, the func method is called to invoke C# code with the following line:

var helloWorld = edge.func('async (input) => { return ".NET Welcomes " + input.ToString(); }');

This example uses C# with Edge.js currently supporting F#, PowerShell, and Python. The documentation states there are several ways to embed C# code within Node.js source:

  1. You embed a C# literal representing a .NET async lambda expression implementing the Func<object,Task<object>> delegate directly inside Node.js code. This is what was done in the previous example.
  2. You can embed multiline C# source code by providing a function with the body containing multi-line comments; this can be a single function or more extensive code like a complete class definition. With classes it gets more complicated, as it must be named Startup and have an Invoke method. The online documentation provides plenty of details.
  3. You can simply reference a file containing your source code.

Edge.js will compile your code on the fly unless specified otherwise. Again, the documentation provides plenty of details.

Calling Node.js from .NET

To use Node.js code within your .NET code, it requires a different setup. Edge is installed via NuGet, which can be achieved via the NuGet Package Manager console (Figure D) or the NuGet Package Manager window (Figure E). I had to install the ScriptCS.Edge package to get Edge.js to work on my development machine -- the Edge.js package is one part of the install.

Figure D

Installed Edge.js via the NuGet Package Manager console.

Figure E

Installing Edge.js via the NuGet Package Manager window.

With it installed, you can begin integrating Node.js within the .NET application. Figure F shows an example that utilizes a simple Node.js application that displays text. The using statement references the EdgeJs library with its Edge object used to invoke Node.js code via the Func method.

Figure F

Calling Node.js code from a C# console application.

A solution looking for a problem

After working through numerous online examples and browsing user discussions, I think the integration is impressive. The ability to utilize ADO.NET on the backend to interact with a backend database is what I find the most intriguing.

I only dove into Edge.js out of curiosity (none of my Node.js-based projects have needed such integration), but now that I'm aware of the solution, I'm better prepared for future projects that may need such integration. Other integration approaches such as COM interop for using older components were a nightmare, but the past doesn't always dictate or predict the future, so I'll continue working with Edge.js to get a better feel for how it performs.

If you've used Edge.js, what do you think of it? Let us know in the discussion.

Also read