4

You can see the interop model for going from Node.js -> C#, here.

What I want to know is, can the C# code then make a call to a method in the Node.js part of the process from the C#, before returning?

Imagine if you had a call, like

var webApi = edge.func('/MyDotNetApi.csx');
webApi(function (error, result) { log.('api started'); });

where the MyDotNetApi.csx returns, but leaves a socket listener thread running to handle HTTP requests. Now, if the Node.js part of the process holds (ever changing) information which the .Net code needs to access for inclusion in its HTTP responses, can it somehow ask Node.js for it?

Maggie
  • 1,546
  • 16
  • 27
Jordan Morris
  • 2,101
  • 2
  • 24
  • 41

1 Answers1

2

Calling back Node.js from C# with Edge.js is possible and documented.

noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Do you know if the resources passed into the closure are responsive to changes in the node process? E.g. could you pass in a function to the closure which accesses a node global variable and see updates to the global? – Jordan Morris Sep 25 '13 at 04:26
  • I'm not an expert in Node, but I think that should work. The important thing here is that the whole chain of calls (Node -> C# -> Node) must be executed on the same thread. Make sure you don't switch threads inside C# before you do the callback. Thus, you'd simply re-enter the Node context on a nested stack frame and you'd want to return from the callback as soon as possible, to avoid blocking the Node event loop. – noseratio Sep 25 '13 at 04:37
  • 2
    Edge.js passes data between V8 and CLR by value, except for functions. If Node.js controls ever changing data that it wants to expose to .NET, it can do it in one of two ways: (1) by exposing a function from Node.js to .NET that allows .NET code to *query* for the current value as needed - [example](https://github.com/tjanczuk/edge/blob/master/samples/108_func.js#L19-L21), or (2) by having .NET code return a function to Node.js which Node.js will *invoke* whenever the value of the data changes - [example](https://github.com/tjanczuk/edge/blob/master/samples/111_clr_listener.js#L20-L23). – Tomasz Janczuk Dec 13 '13 at 08:03