What is the difference between dnode and nowjs?

2019-01-29 20:28发布

问题:

How do the two compare to each other?

回答1:

TL;DR

DNode

  • provides RMI;
  • remote functions can accept callbacks as arguments;
  • which is nice, since it is fully asynchronous;
  • runs stand-alone or through an existing http server;
  • can have browser and Node clients;
  • supports middleware, just like connect;
  • has been around longer than NowJS.

NowJS

  • goes beyond just RMI and implements a "shared scope" API. It's like Dropbox, only with variables and functions instead of files;
  • remote functions also accept callbacks (thanks to Sridatta and Eric from NowJS for the clarification);
  • depends on a listening http server to work;
  • can only have browser clients;
  • became public very recently;
  • is somewhat buggy right now.

Conclusion

NowJS is more of a toy right now -- but keep a watch as it matures. For serious stuff, maybe go with DNode. For a more detailed review of these libraries, read along.

DNode

DNode provides a Remote Method Invocation framework. Both the client and server can expose functions to each other.

// On the server

var server = DNode(function () {
    this.echo = function (message) {
        console.log(message)
    }
}).listen(9999)

// On the client

dnode.connect(9999, function (server) {
    server.echo('Hello, world!')
})

The function that is passed to DNode() is a handler not unlike the one passed to http.createServer. It has two parameters: client can be used to access the functions exported by the client and connection can be used to handle connection-related events:

// On the server

var server = DNode(function (client, connection) {
    this.echo = function (message) {
        console.log(message)
        connection.on('end', function () {
            console.log('The connection %s ended.', conn.id)
        })
    }       
}).listen(9999)

The exported methods can be passed anything, including functions. They are properly wrapped as proxies by DNode and can be called back at the other endpoint. This is fundamental: DNode is fully asynchronous; it does not block while waiting for a remote method to return:

// A contrived example, of course.
// On the server

var server = DNode(function (client) {
    this.echo = function (message) {
        console.log(message)
        return 'Hello you too.'
    }
}).listen(9999)

// On the client

dnode.connect(9999, function (server) {
    var ret = server.echo('Hello, world!')
    console.log(ret) // This won't work
})

Callbacks must be passed around in order to receive responses from the other endpoint. Complicated conversations can become unreadable quite fast. This question discusses possible solutions for this problem.

// On the server

var server = DNode(function (client, callback) {
    this.echo = function (message, callback) {
        console.log(message)
        callback('Hello you too.')
    }

    this.hello = function (callback) {
        callback('Hello, world!')
    }
}).listen(9999)

// On the client

dnode.connect(9999, function (server) {
    server.echo("I can't have enough nesting with DNode!", function (response) {
        console.log(response)
        server.hello(function (greeting) {
            console.log(greeting)
        })
    })
})

The DNode client can be a script running inside a Node instance or can be embedded inside a webpage. In this case, it will only connect to the server that served the webpage. Connect is of great assistance in this case. This scenario was tested with all modern browsers and with Internet Explorer 5.5 and 7.

DNode was started less than a year ago, on June 2010. It's as mature as a Node library can be. In my tests, I found no obvious issues.

NowJS

NowJS provides a kind of magic API that borders on being cute. The server has an everyone.now scope. Everything that is put inside everyone.now becomes visible to every client through their now scope.

This code, on the server, will share an echo function with every client that writes a message to the server console:

// Server-side:

everyone.now.echo = function (message) {
    console.log(message)
}

// So, on the client, one can write:

now.echo('This will be printed on the server console.')

When a server-side "shared" function runs, this will have a now attribute that is specific to the client that made that call.

// Client-side

now.receiveResponse = function (response) {
    console.log('The server said: %s')
}

// We just touched "now" above and it must be synchronized 
// with the server. Will things happen as we expect? Since 
// the code is not multithreaded and NowJS talks through TCP,
// the synchronizing message will get to the server first.
// I still feel nervous about it, though.

now.echo('This will be printed on the server console.')

// Server-side:

everyone.now.echo = function (message) {
    console.log(message)
    this.now.receiveResponse('Thank you for using the "echo" service.')
}

Functions in NowJS can have return values. To get them, a callback must be passed:

// On the client

now.twice(10, function (r) { console.log(r) }

// On the server

everyone.now.twice = function(n) {
    return 2 * n
}

This has an implication if you want to pass a callback as an honest argument (not to collect a return value) -- one must always pass the return value collector, or NowJS may get confused. According to the developers, this way of retrieving the return value with an implicit callback will probably change in the future:

// On the client

now.crunchSomeNumbers('compute-primes', 

    /* This will be called when our prime numbers are ready to be used. */

    function (data) { /* process the data */ }, 

    /* This will be called when the server function returns. Even if we
    didn't care about our place in the queue, we'd have to add at least
    an empty function. */

    function (queueLength) { alert('You are number ' + queueLength + ' on the queue.') }
)

// On the server

everyone.now.crunchSomeNumbers = function(task, dataCallback) {
    superComputer.enqueueTask(task, dataCallback)
    return superComputer.queueLength
}

And this is it for the NowJS API. Well, actually there are 3 more functions that can be used to detect client connection and disconnection. I don't know why they didn't expose these features using EventEmitter, though.

Unlike DNode, NowJS requires that the client be a script running inside a web browser. The page containing the script must be served by the same Node that is running the server.

On the server side, NowJS also needs an http server listening. It must be passed when initializing NowJS:

var server = http.createServer(function (req, response) {
    fs.readFile(__dirname + '/now-client.html', function (err, data) {
        response.writeHead(200, {'Content-Type':'text/html'})  
        response.write(data)
        response.end()
    })
})
server.listen(8080)
var everyone = now.initialize(server)

NowJS first commit is from a couple weeks ago (Mar 2011). As such, expect it to be buggy. I found issues myself while writing this answer. Also expect its API to change a lot.

On the positive side, the developers are very accessible -- Eric even guided me to making callbacks work. The source code is not documented, but is fortunately simple and short and the user guide and examples are enough to get one started.



回答2:

NowJS team member here. Correction to andref's answer:

NowJS fully supports "Remote Method Invocation". You can pass functions as arguments in remote calls and you can have functions as return values as well.

These functions are wrapped by NowJS just as they are in DNode so that they are executed on the machine on which the function was defined. This makes it easy to expose new functions to the remote end, just like in DNode.

P.S. Additionally, I don't know if andref meant to imply that remote calls are only asynchronous on DNode. Remote calls are also async on NowJS. They do not block your code.



回答3:

Haven't tried Dnode so my answer is not a comparison. But I would like to put forth few experiences using nowjs.

Nowjs is based on socket.io which is quite buggy. I frequently experience session time-outs, disconnects and now.ready event firing multiple times in a short duration. Check out this issue on nowjs github page.

Also I found using websockets unviable on certain platforms, however this can be circumvented by explicitly disabling websockets.

I had planned creating a production app using nowjs but it seems its not mature enough to be relied upon. I will try dnode if it serves my purpose, else I will switch to plain-old express.

Update:

Nowjs seems to be scrapped. No commits since 8 months.