Node.Js in Erlang style?
Asked Answered
D

4

7

I am a complete noob when it comes to both Node.Js and Erlang. But wouldn't it be possible to build a Node.js app that emulates Erlang behavior?

e.g. you pass json messages across an distributed node.js server park and even pass new code to those servers w/o going offline, just like erlang.

If you have a message handler callback that is activated when a message is received, then this message handler could check if the message is a code update message and thus replace itself(the current handler) with the new code.

So it should be possible to have Node.Js servers with no downtime for code updates w/o too much fuss, right?

Drugstore answered 14/12, 2010 at 9:25 Comment(2)
I can't speak to code updates but I just found a Node library modeled after Erlang's OTP supervisor.Bolan
Hm. 4 years ago. I kind of love this idea.Ferree
C
5

Not completely right.

  1. Yes you could distribute JSON messages
  2. The part with hot code replacement is a bit more complicated let me explain...

OK, first you obviously need to have validation etc. in place, that shouldn't be a big problem. The first small problem arises from JSON, which does not allow for any JS code/functions in it, well you can work around that by sending the data as a string.

Next problem, when you want to replace function/method you need to make sure that it keeps it's scope, so that the newly compiled functions has access to the same things.

With some dark eval magic this is certainly possible, but don't expect it to be anywhere near as natural as it's in Erlang:

var Script = process.binding('evals').Script;

var hello = 'Hello World';
var test = 42;
function Swappable(initCode) {
    this.execute = function() {}
    this.swap = function(code) {
        this.execute = eval('func = ' + code);
    }
    this.swap(initCode);
}

// Note: Swappable's scope is limited, it won't inherit the local scope in which it was created...
var foo = new Swappable('function(){console.log(hello);return function(){console.log(test)}}')
var cb = foo.execute();
cb();

foo.swap('function(){console.log("Huh, old world?");return function(){console.log(test * test)}}');
var cb = foo.execute();
cb();
console.log(bar.execute());
foo.execute();

Output

Hello World
42
Huh, old world?
1764

This is not guaranteed to work in 100% of all cases and scopes. Also, the syntax is horrible, so I'd suggest if you want hot swapping, stay with Erlang.

Remember: Right tool for the right job.

Update
There won't be anything better than that in the near future see:
https://github.com/ry/node/issues/issue/46#issue/46/comment/610779

Choreographer answered 14/12, 2010 at 10:21 Comment(0)
M
2

Here is a blog comparing experiences using Erlang and Node.js:

http://blog.mysyncpad.com/post/2073441622/node-js-vs-erlang-syncpads-experience

Here is another comparison which purposefully does not compare speed as such:

http://jlouisramblings.blogspot.com/2010/12/differences-between-nodejs-and-erlang_14.html

Maratha answered 14/12, 2010 at 13:44 Comment(1)
To be honest that post is very misleading, first of all there's no mention about which version of Node.js he used. It's widely known that 2.x has massive problems when pushing strings to sockets (exactly what he did) and that there are some bugs in V8 with JSON encode/decode. The string problem is fixed in newer 3.x builds.Choreographer
S
1

Don't have enough points to comment inline, but I wanted to respond to Ivo Wetzel's comment above at rvirding's post. There is an updated blog on mysyncpad where the author uses the version of nodejs specifically recommended by the nodejs and v8 developers.

http://blog.mysyncpad.com/post/2143658273/syncpad-node-js-server-revisited

Skeleton answered 15/12, 2010 at 19:25 Comment(0)
J
0

I assume With script module you could execute javascript without reloading the server.

supervisor

A little supervisor script for nodejs. It runs your program, and watches for code changes, so you can have hot-code reloading-ish behavior, without worrying about memory leaks and making sure you clean up all the inter-module references, and without a whole new require system.

But then again it will reload(very short time offline) when it detects file changes.

Jostle answered 14/12, 2010 at 9:59 Comment(3)
With Erlang style messages you could hot swap w/o reloading anything. you could even transform local state before activating the new message loopDrugstore
I also found this when googling => romeda.org/blog/2010/01/hot-code-loading-in-nodejs.html. But also believe ryan wants to put this in the codebase(but then again he is a busy man). but for now I don't believe it is as advanced as erlang?Jostle
I checked it out, this does not preserver module state, so all you can use it for is patching of static, stateless utilities.Choreographer

© 2022 - 2024 — McMap. All rights reserved.