Just a guy in Vermont trying to connect all the dots...
Author's posts
Feb 04
Video: How to use the new debugger client in Node.js
How does the new debugger work in the latest version of Node.js? my colleague Chris Matthieu recently pointed me to this screencast from Ryan Dahl. It looks cool…
New built-in V8 debugger client for Node.js from ry on Vimeo.
Feb 04
Using PostBin to Debug Webhooks
Recently I was trying to debug a python app that ran on Tropo.com Scripting and made a web call back to an application on another server. I wanted to see exactly what was being sent in the HTTP POST rom Tropo to the other server… and that’s when my colleague Mark Headd pointed me over to PostBin.org:
PostBin is very cool because literally all you do is click the “Make a PostBin” button and then you get a URL that looks like
http://www.postbin.org/1dlalso
Now you just do a HTTP POST to that URL and… ta da… the results of your post appear on the new webpage.
It’s a very cool way to debug webhooks!
P.S. Obviously there is no language limitation here. I happened to use python because that was what I was debugging, but you can use PostBin with ANY language that is sending HTTP connections.
Feb 03
Textastic – a great iPad code editor
Lately I have been looking for a code editor for the iPad … and am VERY impressed so far by Textastic. It is not a free app, but it has been well worth the money already. with it, i was able to very easily create the rough draft of an HTML email newsletter on my flight down to Florida… i have also done some codinv in it…
The are a range of great featurea, but the most prominent one is the simple extra row on top of the keyboard that gives you characters commonly used in programming. HUGE timesaver! as shown in the image below, you can work with multiple files… all in all I am extremely impressed. Many thanks to the friends on Twitter who suggested it!
Jan 29
Using LESS To Create Dynamic Stylesheets with Node.js
Have you been working with Cascading Style Sheets (CSS) and found yourself wishing you could make those sheets more, oh, dynamic? Like, for instance, including variables so that you could easily change colors or spacing throughout a stylesheet? There are a number of solutions out there to do this, but one I stumbled upon recently is “LESS“. Here’s a graphic from the LESS site that shows exactly this usage with variables:

You can also do some very cool things with easily nesting rules and creating functions that can calculate values for other rules.
All in all it looks very cool… and it can run either in a client browser or, of more interest to me, server-side using Node.js. I have a site idea where this just might come in handy…
P.S. Any other modules for Node.js for doing stylesheet creation that you all like?
Jan 28
Meet the Author and Discuss UC Security – Next Week In Miami
As I mentioned on both the VOIPSA blog and my Disruptive Telephony site, I'll be in Miami next week, February 2-4, speaking at the SIP Trunking Workshop and Cloud Communications Summit about Unified Communications security.
If you are there at any of the events in Miami (my schedule is online), please do say hello... and if you'd like to meet, please send me an email or contact me on Twitter.
P.S. I may have a few books with me... ;-)
Jan 26
Test Out Python… Directly In Your Web Browser
Pretty cool little site to test out python directly inside your web browser (found via Hacker News):
You just type in some python code and press the “Execute” button. Now, sure, it doesn’t have support for much outside the very core language… and you could get all of that by simply flipping to a terminal window and using python from the command line.
Still, it’s a fun way to be able to show people new to python what they can do… and a cool proof-of-concept of running python directly inside your web browser!
Jan 25
Node.js, Doctor’s Offices and Fast Food Restaurants – Understanding Event-driven Programming
Are you struggling to understand “event-driven” programming? Are you having trouble wrapping your brain around “blocking” vs “non-blocking” I/O? Or are you just trying to understand what makes Node.js different and why so many people are talking about it? (and why I keep writing about it?)
Try out one of these analogies…
The Doctor’s Office Reception Line Analogy
In the excellent episode 102 of the Herding Code podcast, Tim Caswell relates event-driven programming to standing in the line at a doctor’s office to see the receptionist. Inevitably, at least here in the USA, there are additional forms to fill out with insurance info, privacy releases, etc.
A Traditional Model
In a traditional thread-based system, when you get to the receptionist you stand at the counter for as long as it takes you to complete your transaction. If you have to fill out 3 forms, you would do so right there at the counter while the receptionist just sits there waiting for you. You are blocking her or him from servicing any other customers.
The only real way to scale a thread-based system is to add more receptionists. This, however, has financial implications in that you have to pay more people and physical implications in that you have to make the room for the additional receptionist windows.
Events To The Rescue
In an event-based system, when you get to the window and find out you had to complete additional forms, the receptionist gives you the forms, a clipboard and a pen and tells you to come back when you have completed the forms. You go sit down in the waiting and the receptionist helps the next person in line. You are not blocking her from servicing others.
When you are done with your forms, you get back in line and wait to speak with the receptionist again. If you have done something incorrectly or need to fill out another form, he or she will give you the new form or tell you the correction and you’ll repeat the process of going off, doing your work, and then waiting in line again.
This system is already highly scalable (and is in fact used in most doctor’s offices I visit). If the waiting line starts getting too long, you can certainly add an additional receptionist, but you don’t need to do so at quite the rate of a thread-based system.
The Fast Food Restaurant Analogy
It struck me that this is also very similar to ordering your food at a fast-food restaurant or street vendor.
The thread-based way would be to get to the front of the line, give your order to the cashier and then wait right there until your order was cooked and given to you. The cashier would not be able to help the next person until you got your food and went on your way. Need to service more customers… just add more cashiers!
Of course, we know that fast food restaurants don’t work that way. They are very much event-driven in that they try to make those cashiers as efficient as possible. As soon as you place your order, it’s sent off for someone to fulfill while the cashier is still taking your payment. When you are done paying, you have to step aside because the cashier is already looking to service the next customer. In some restaurants, you might even be given a pager that will flash and vibrate when your order is ready for pickup (My local Panera Bread does this). The key point is that you are not blocking the receiving of new orders.
When your food is set, the cashier – or someone – will signal you by calling out your name, order number or triggering your pager. The event of your order being ready causes the person to perform some function/action. (In programming lingo, this would be thought of as a “callback function“.) You will then go up and get your food.
So What Does This Have To Do With Node.js?
The “traditional” mode of web servers[1] has always been one of the thread-based model. You launch Apache or any other web server and it starts receiving connections. When it receives a connection, it holds that connection open until it has performed the request for the page or whatever other transaction was sent. If it make take a few microseconds to retrieve a page from disk or write results to a database, the web server is blocking on that input/output operation. (This is referred to as “blocking I/O“.) To scale this type of web server, you need to launch additional copies of the server (referred to as “thread-based” because each copy typically requires another operating system thread).
In contrast, Node.js uses an event-driven model where the web server accepts the request, spins it off to be handled, and then goes on to service the next web request. When the original request is completed, it gets back in the processing queue and when it reaches the front of the queue the results are sent back (or whatever the next action is). This model is highly efficient and scalable because the web server is basically always accepting requests because it’s not waiting for any read or write operations. (This is referred to as “non-blocking I/O” or “event-driven I/O“.)
To put it a bit more concretely, consider this process:
- You use your web browser to make a request for “/about.html” on a Node.js web server.
- The Node server accepts your request and calls a function to retrieve that file from disk.
- While the Node server is waiting for the file to be retrieved, it services the next web request.
- When the file is retrieved, there is a callback function that is inserted in the Node servers queue.
- The Node server executes that function which in this case would render the “/about.html” page and send it back to your web browser.
Now, sure, in this case, it may only take microseconds for the server to retrieve the file, but..
microseconds matter!
Particularly when you are talking about highly-scalable web servers!
This is what makes Node.js different and of such interest right now. Add in the fact that it also uses the very common language of JavaScript, and it is a very easy way for developers to create very fast and very scalable servers.
Do these analogies help? Do have another analogy you use to explain “event-driven programming” or “event-driven I/O”?
[1] While I’m talking about “web servers” here, Node.js lets you write all sorts of different types of servers for many other protocols beyond HTTP. They all have similar issues (blocking vs non-blocking I/O).
Image credit: gerry balding on Flickr
Jan 22
Slides: Using Node.js in a production setting
Interesting set of slides around using Node.js in a production setting – and the choices these folks made around the technology stack they used:
Jan 21
How to use Node.js with IPv6
It turns out to be ridiculously simple. After my testing yesterday, I learned via a comment from Ryan Dahl, that:
all you do is have your Node.js app listen on the IPv6 address.
That’s it.
UPDATE 28 Jul 2014 – As a result of a Reddit thread I’ve seen traffic to this post. Please do note that I wrote this post in early 2011 when I was experimenting with Node.js. I’ve not kept up with the state of Node.js and so 3 years later there may be new or better ways to do what I’ve listed here. Also, please see the comment from Simon Vetter indicating that you don’t need to start up two servers if you simply use “::” as the address. At some point I need to verify that and then update this post.
In a typical application using, say, the “http” module, you will have a line somewhere that says something like:
[js]
server.listen(80);
[/js]
This will bind to your default network interface and address, which, for pretty much all of us, will be our IPv4 address.
To bind to an IPv6 address, you just need to supply the address as an argument to listen(). For example:
[js]
server.listen(80, "2001:db8:1111:2222:3333::51"]
[/js]
So a very basic Node.js app using IPv6 would look like this:
[js]
var http = require(‘http’);
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type":"text/plain"});
response.end ("Hello World!n");
console.log("Got a connection");
});
server.listen(80, "2001:db8:1111:2222:3333::51");
console.log("Server running on localhost at port 80");
[/js]
Note that as a Node.js app like this will only bind to a single interface and address, this app only listens on the IPv6 address and not on the IPv4 address. To make the app work on both the IPv4 and IPv6 addresses, I need to refactor it a bit into a function that is then called by two different servers that attach to the two network interfaces:
[js]
var http = require(‘http’);
var handler = function (request, response) {
response.writeHead(200, {"Content-Type":"text/plain"});
response.end ("Hello World!n");
console.log("Got a connection");
};
var server6= http.createServer();
server6.addListener("request",handler);
server6.listen(80,"2001:db8:1111:2222:3333::51");
var server= http.createServer();
server.addListener("request",handler);
server.listen(80,"192.0.2.23");
console.log("Server running on localhost at port 80");
[/js]
With that in place, it’s now answering on both the IPv4 and IPv6 addresses.
Now this is for the Node.js base modules… other modules may or may not “just work” for IPv6, too, depending upon what they rely on underneath. Regardless, it’s rather cool that it works so easily on IPv6. Kudos to the Node.js team for making it so simple!




