node.js: First thoughts
I recently came across node.js via a blog post by Paul Gross and I’ve been playing around with it a bit over the weekend trying to hook up some code to call through to the Twitter API and then return the tweets on my friend timeline.
Simon Willison has part of a presentation on slideshare where he describes the difference between the typical thread per request approach and the event based approach to dealing with web requests using the metaphor of bunnies. He also has a blog post where he describes this is more detail.
Another resource I found useful is a video from jsconf.eu where the creator of node.js, Ryan Dahl, explains the philosophy behind event driven I/O and gives several examples using node.js.
These are some of my thoughts so far:
- I'm not used to have so many callbacks spread all around the code and I'm still getting used to the idea that they aren't executed until the event actually happens! I often find myself looking at a piece of code and not understanding how it can possibly work because I'm assuming that the function passed in is executed immediately when in fact it isn't.
- It seems like node.js would be perfect for a version of my colleagues Julio Maia and Fabio Lessa's http-impersonator which is a Java application used to record and replay requests/responses made across http-based protocols. I haven't quite worked out the best way to test the above code - ideally I want to stub out the HTTP request so that the test doesn't have to go across the wire. Micheil Smith pointed me towards fakeweb which allows the faking of HTTP requests/responses in Ruby so I'll probably have a go at creating something similar.
So far node.js seems really cool and writing code using it is really fun. I’m still not sure exactly where it will fit in some of the architectures that I’ve worked on but the model it encourages feels really natural to work with.