He didn't post source code (as far as I could tell, after searching for two minutes :) but I was able to cobble something together yesterday along the same lines.
Here's the client:
and server:
This works with Guille's node.websocket.js library. Start with that, then add chat.js into ./modules.
One last tweak is you have to add onDisconnect to node.websocket.js. I did that by adding the following:
if (this.module.onConnect) {
this.module.onConnect(this);
}
to the end of Connection.prototype._handshake. I suppose it could be called from elsewhere but that seems to work.
Check out amix's blog - lots of interesting details about how Plurk scales out to over 100,000 open connections. Hint: you don't do it with one thread per connection. The Servlet/CGI model is starting to look pretty old and creaky these days.
Update: bru has a node.websocket.js fork that includes a comet chat app. It's more complete than what I've posted here and he appears to have worked around the missing onConnect by just adding new connection objects in the onData callback.

8 comments: