Get ready to dive into the “N” of MEAN — Node.js! While Node.js is only one part of the MEAN framework (MongoDB, Express, Angularjs, Nodejs), it’s certainly one of its most dynamic (and fastest) components.
Ok, so let’s break down what that means.
San Jose instructor Speros Misirlakis explains Node servers this way: “Node.js provides the ability to have asynchronous events with Socket.IO. That’s giving you the ability to create applications where everybody sees updates from one client or user, live in the moment.”
Being driven by asynchronous design means it’s really damn fast. It can also handle millions of concurrent connections at once, which is pretty awesome.
For example, let’s say you tried to book a plane ticket and the website says two tickets are left. You click on the “Book It” button and the response says, ‘Sorry, the tickets have run out.’
That, on the other hand, is not so awesome.
A Node server essentially eliminates the need to refresh the site, and all users see how many tickets are left in real-time. It provides a WAY better experience for the end user.
Event-driven vs. Thread-based
To understand the key benefit of Node servers and why they’re quickly gaining ground on Apache servers, let’s wrap our heads around non-blocking I/O processing with an analogy.
Spoiler alert: it’s all about speed and scalability.
Thread-based web servers
Imagine waiting in line at the DMV. When it’s finally your turn, you arrive and talk to the clerk at the counter for as long as it takes to complete your transaction. You fill out forms, provide information and write checks at the counter, preventing the clerk from helping any other customers. This is comparable to the web server blocking the input/output operation (e.g., blocking I/O).
That is essentially a traditional, thread-based server model. When a clerk (or a web server) receives a connection, it keeps that connection open until it has fully completed the request.
To scale a thread-based system, the DMV could hire more clerks; however, similar to scaling servers for increased loads, extravagant costs are associated with hiring and managing additional clerks.
Event-driven web servers
On the other hand, if the DMV used an event-driven model (which they mostly don’t, hence all the hate for the DMV), you would approach a clerk, who would then hand you a clipboard and pen. You would return to the clerk when you completed the forms, thus avoiding blocking anyone from receiving the clerk’s help (e.g., non-blocking I/O).
If the wait is still too long to talk with a clerk, you can scale and add clerks, but not nearly at the rate needed in a thread-based system.
In this event-driven server model, the web server accepts the request, spins it off to be handled, and then goes on to service the next request. When the original request is completed, it gets back in the processing queue; when it reaches the front of the queue, the results are sent back.
The discrepancy between event-driven and thread-based servers is usually only microseconds—but trust us, microseconds really matter when you’re dealing with highly scalable web servers.
A more technical explanation…
Dan York, content strategist at InternetSociety.org, explains the process more technically:
- 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 the disk.
- While the Node server is waiting for the file to be retrieved, it services the next web request.
- When the file is retrieved, a callback function is inserted in the Node server’s 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.
Getting Busy With Node.js
Ready to see Node.Js in action?
Head on over to the Hummingbird project, which provides real-time web traffic visualization. WebSockets built on top of Node.js enables Hummingbird to update a mind-boggling 20 times per second.
You can also visit the official website for Node, or check out some of these helpful resources to get crackin’:
It’s not surprising that our students fall in love with Node.js, with many of them incorporating them into portfolio pieces.
MEAN is full of surprises. Head on over to this blog post to learn about the rest of the stack in action.