There was an engineer I used to work with who always said "That's just a software issue." No matter how complex the issue, no matter how daunting, they were confident it could be solved with software.
.NET and C# and NuGet and the community have been making some amazing stuff in the last few years like ScriptCS, Chocolately, Boxstarter. Azure Websites now supports ASP.NET, sure, but also PHP, Python, Java (Tomcat or Jetty or your own container), and node.js. Getting these things to work together has been an interesting software issue. Apps can run side-by-side, but they can't really talk to each other in-process. (Mostly one just moves data between universes over JSON and HTTP when need-be.)
However, Tomasz Janczuk has been working on Edge.js (on Github) for a while now. I showed his work at jQuery Portland last year, but this week he's taking it to the next level. He is creating a wormhole between software universes.
Edge.js now lets you run node.js and .NET code in-process on Windows, Mac, and Linux.
The name is great. An edge connects two nodes, and Edge.js is that edge.
Here's a node app hello world node app calling .NET. Don't sweat that the .NET code is tunneled inside a comment, this is the Hello World proof of concept.
var edge = require('edge');
var helloWorld = edge.func(function () {/*
async (input) => {
return ".NET Welcomes " + input.ToString();
}
*/});
helloWorld('JavaScript', function (error, result) {
if (error) throw error;
console.log(result);
});
Perhaps you have a bunch of CPU intensive work or algorithms in C#, but you've also got a node.js app that needs the result of that work. Edge can help with that.
You can bring in a CS or CSX file into node like this:
var myCSharpCode = edge.func(require('path').join(__dirname, 'myCSharpCode.csx'));
You can bring code from a .NET DLL into a node.js compiled as well.
var clrMethod = edge.func({
assemblyFile: 'My.Edge.Samples.dll',
typeName: 'Samples.FooBar.MyType',
methodName: 'MyMethod'
});
It's not a hack, it's a clear way to marshal between CLR threads and the V8 (the node Javascript engine) thread. It's also interesting from a comp-sci perspective as the CLR can have many threads and V8 has the one.
Here's Tomasz's own words:
Edge.js provides an asynchronous, in-process mechanism for interoperability between Node.js and .NET.
You can use this mechanism to:
- access MS SQL from Node.js using ADO.NET more...
- use CLR multi-threading from Node.js for CPU intensive work more...
- write native extensions to Node.js in C# instead of C/C++
- intergate existing .NET components into Node.js applications
Read more about the background and motivations of the project here.
Now, you might ask yourself, what problem does Edge.js solve? The answer is in the Edge.js FAQ.
Go explore what you can do. Edge goes much further than just C# and Node. It works on Windows, OSX, and Ubuntu but you should just "npm install edge" as there's a node package available.
- How to: integrate C# code into Node.js code
- How to: specify additional CLR assembly references in C# code
- How to: marshal data between C# and Node.js
- How to: call Node.js from C#
- How to: export C# function to Node.js
- How to: script Python in a Node.js application
- How to: script PowerShell in a Node.js application
- How to: script F# in a Node.js application
- How to: script T-SQL in a Node.js application
- How to: support for other CLR languages
- How to leverage SQL Server with Node.js using Edge.js
Have fun! You have a lot more power and flexibility than you think. It's just a software problem.
© 2014 Scott Hanselman. All rights reserved.