Node.js: Five Things Every PHP Developer Should Know
I recently started working on a few Node.js applications. Coming most recently from PHP (and Drupal in particular), I found the transition to Node.js to be surprisingly easy. Pleasurable, in fact. But I had to learn to think differently about a few things.
Below I list the five things I think every PHP developer should know about Node.js. <!--break-->
This has several positive implications for you, the developer:
- With V8's JIT (Just In Time) compiling, apps run at near-native speeds (benchmarks indicate it's much faster than PHP and Ruby, in terms of running analogous computational tasks).
- As V8 improves, Node will too.
2. Node.js Isn't (Just) A Web Server or Platform
But you can do much more with Node. It is easy to build command line clients and other TCP/IP servers.
On the one hand, this is great news. Node.js is so flexible.
On the other hand, since Node.js isn't HTTP-centric, you may find yourself having to implement code to do things once provided for you by the framework. In other words, in node, there is no
4. Evented I/O?
Now we're to the most controversial aspect of Node.js. Node itself runs in one thread. ONE! (Compare this to your typical Apache/PHP system where a dozen or more PHP instances are running at once.) Yet somehow it is fast and efficient.
What's the secret? Sharing execution time, and offloading intensive IO processes to other threads.
I could go off on a long jargon-filled tangent about the benefits and drawbacks of "evented I/O", but instead I'll stick to the practical: When writing in Node.js, you need to think a little harder about whether your task is slow (and I/O bound) or fast. Use asynchronous functions with callbacks or event handlers for the slow work.
The important thing is to make sure that your application code doesn't allow one request to monopolize the main Node process for too long without giving opportunities for other requests to get some work done.
5. Package Management is a Must!
Be honest. Do you love PEAR? Do you turn almost all of your code into PEAR or PECL packages? Not that many PHP developers do (and a surprising number of them don't even know what PEAR packages are!).
You probably don't want to carry that mentality over to Node.js.
- Node.js is designed to be a minimalistic framework. 90% of the stuff you find in PHP's core will not be present in Node.js's core. Need an example or two? Database drivers? Not in Node's core. Mail libraries? Not in Node's core. HTML support? Not in Node's core.
- But a modular architecture is in Node's core. And you will use it because it is awesome.
npmtool (Node Package Manager) is the second thing you should download -- right after Node. With it, a world of Node.js libraries will be available to you. Drivers, parsers, formatters, servers... there are thousands of packages.
- Building and publishing your own packages is dead simple. I released my first one only a few days after starting with Node. It's just that easy.
If you're a Drupal developer, you can think about Node's packaging system as something similar to Drupal modules -- but with the developer (and not site-builder) in mind.
Conclusion: Ask You Doctor if Node.js is Right for You
I've highlighted the things about Node.js that I think that PHP developers ought to know. And the tone of this article is obviously positive. Node.js is an interesting product with an elegant architecture.
But I'm not going to suggest that Node is the solution to every problem. I'm not going to suggest that Node will immediately replace PHP (many things would need to change in Node before that is a possibility).