Looking through the nowjs source code, I believe they do this by continuously monitoring the now
object and pushing changes between client and server whenever they are detected. I admit I haven’t fully grokked their code yet, however.
In a browser, this would be done with some fun setInterval
hacks.
EDIT: yes, that is indeed what they do: line 368 of the client now.js
. They do some more tricks so that once a new property is detected, future access to it is caught by getters and setters, but those modifications are only made every 1000 ms in a setTimeout
.
Another piece of evidence that this is impossible in current JavaScript is that the proxies proposal for ECMAScript Harmony is designed explicitly to enable such scenarios, implying very strongly that they can’t be done currently. Recent Mozilla browsers have a prototype proxies implementation, if perhaps that’s enough. And apparently V8 is working to add support, which could be enough depending on what version of V8 Node is using these days.
EDIT2: oh cool, on the server side apparently nowjs does use proxies! Which likely means they are mature enough in Node for your usage. See what they do at https://github.com/Flotype/now/blob/master/lib/proxy.js. Or just do var Proxy = require("nodejs-proxy")
and hope they follow the spec so you can take advantage of the documentation from MDC and elsewhere.