Not completely right.
- Yes you could distribute JSON messages
- 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