27
Why The Hell Would I Use Node.js? A Case-by-Case Tutorial

Why the Hell Would I Use Node.js?

  • Upload
    toptal

  • View
    42

  • Download
    0

Embed Size (px)

Citation preview

Why The Hell Would I Use Node.js?A Case-by-Case Tutorial

Introduction

• JavaScript’s popularity changed web development dramatically • Running a Javascript on the server was hard to imagine just several years ago

• A good read before digging into Node.js

JavaScript across the stack

Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written

in JavaScript

Definition by Wikipedia:

Aim was to create a real-time websites with push capability, “inspired by applications like Gmail”

Node.js

•Shines in real-time web applications employing push technology over websockets

•Offers developers a tool for working in the non-blocking, event-driven I/O paradigm

• It is based on the open web stack (HTML, CSS and JS), and is running over the standard port 80

Node.js (cont.)

•Non-blocking, event-driven I/O •Remains lightweight and efficient in the face of data-intensive real-time applications

• It is not a silver-bullet • It is a platform that fills a particular need

How Does It Work?

• Each connection (request) spawns a new thread

• It uses a system RAM• Eventually maxing-out

at the amount of available RAM

Traditional web-serving techniques

How Does It Work?

• Allows support a tens of thousands of concurrent connections (held in the event loop).

• Operates on a single-thread

• Uses non-blocking I/O calls

• Achieves scalabilitylevels of over 1M concurrent connections

Node.js

Potential Pitfalls

•Sharing a single thread between all clients requests

•Heavy computation could choke up Node’s single thread

•Avoid exception bubbling up to the core (topmost) Node.js event loop

Examples of Where Node.js Should Be Used

• Chat

• API on top of an Object DB

• Queued Inputs

• Data Streaming

• Proxy

• Brokerage - Stock Trader’s Dashboard

• Application Monitoring Dashboard

• System Monitoring Dashboard

Chat•Chat is the most typical real-time, multi-user application and the sweet-spot example for Node.js:• It is lightweight•High traffic is involved• It is data-intensive•Runs across distributed devices• It is great use-case for learning

Chat (cont.)• The simplest example:

• Server-side

• A simple Express.js application that implements:

• A GET ‘/’ request handler which serves the webpage

• A websockets server that listens for new messages

• Client-side

• An HTML page with a couple of handlers set up:

• ‘Send’ button click event

• Handlers that listens for incoming messages on the websockets client

Chat (cont.)• When client posts a message:

• Browser catches the ‘Send’ button click, picks up the message, and emits a websocket message using the websocket client connected to our server

• Server-side component of the websocket connection receives the message and forwards it to all other connected clients using the broadcast method

• All clients receive the new message as a push message via a websockets client-side component. Browser updates the web page with the new message

API on top of an Object DB

•Object Databases (e.g. MongoDB) allow Node.js to function without the impedance mismatch and data conversion

•With Node.js you can avoid the need for multiple conversions by using a uniform data serialization format across the client, server, and database

Queued Inputs

• Database become a bottleneck when used with high amount of concurrent data

• The solution is to acknowledge the client’s behaviour before the data is truly written to the database

• Data gets queued through some kind of caching or message queuing infrastructure (e.g., RabbitMQ, ZeroMQ) and digested by a separate database batch-write process

• With Node, database writes can be pushed off to the side and deal with them later, proceeding as if they succeeded

Data Streaming

•Traditional web platforms treat HTTP requests and responses like isolated event

•HTTP requests are actually streams•Node can utilize this:• It’s possible to process files while they’re still being uploaded

•This could be done for real-time audio or video encoding

Proxy

•Node.js can handle a large amount of simultaneous connections in a non-blocking manner

• It’s especially useful for proxying different services with different response times, or collecting data from multiple source points

•This is helpful if your proxying infrastructure is non-existent or if you need a solution for local development

Brokerage - Stock Trader’s Dashboard

•Brokers’ trading desktop software could be easily replaced with a real-time web solution

•Switching to a real-time web-based solution to track stocks prices, perform calculations/technical analysis, and create graphs/charts

Application Monitoring Dashboard

• Examples:• Tracking website visitors and visualizing their interactions in real-time

- productized by Hummingbird• Gathering real-time stats from user, introducing targeted interactions with visitors by opening a communication channel when they reach a specific point in your funnel

- productized by CANDDi• Visualize real-time visitors interactions

System Monitoring Dashboard• Examples:• A service-monitoring page that checks the services’ statuses in an asynchronous manner and pushes data to clients using websockets

• Both internal and public services’ statuses can be reported live and in real-time

• Network Operations Center (NOC) monitoring applications in a telecommunications operator, cloud/network/hosting provider, or some financial institution, all run on the open web stack backed by Node.js and websockets instead of Java and/or Java Applets

Where Node.js Can Be Used

• Node.js with Express.js can be used for classic web applications on the server-side

• Pros:

• For non CPU intensive computation, Javascript can be used from top-to-bottom

• More SEO-friendly to crawlers because they receive a fully-rendered HTML response

• Cons:

• Any CPU intensive computation will block Node.js responsiveness, so a threaded platform is a better approach

• Using Node.js with a relational database is still quite a pain, rather use Rails, Django, or ASP.Net MVC

Server-side Web Applications

Where Node.js Shouldn’t Be Used

• Ruby on Rails is better to deal with relational data access• Relational DB tools for Node.js are still in their early stages• If you’re really inclined to remain JS all-the-way:• Sequelize • Node ORM2

• Both are still immature, but they may eventually catch up

Server-side web application w/ a relational DB behind

Where Node.js Shouldn’t Be Used

• Node.js is not the best platform for heavy computation

• Any CPU intensive operation will block any incoming requests while the thread is occupied with number-crunching

• Node.js is single-threaded and uses only a single CPU core

• If you need concurrency on a multi-core server, there is a cluster module or you can run several Node.js server instances behind a reverse proxy via nginx.

• With clustering, offload all heavy computation to background processes and let them communicate via a message queue server like RabbitMQ

Heavy server-side computation/processing

Conclusion

•We’ve covered:•Node.js from theory to practice•goals and ambitions•sweet spots and pitfalls

•Blocking operations are the root of all evil - 99% of Node misuses come as a direct consequence.

Conclusion (cont.)

•Node.js was never created to solve the compute scaling problem

•Node.js was created to solve the I/O scaling problem

Remember:

Welcome to the real-time web!

Bonus Materials

Send a quick email to: [email protected]

• Source code of the sample project• Audio MP3 file to listen on the go• PPT slide deck• PDF file• Original blog post

About the AuthorTomislav Capan, Croatia

Tomislav is a software engineer, technical consultant and architect with over 10 years of experience. He specializes in full-stack, highly scalable, real-time JavaScript and Node.js applications, with past experience in C#, Java, and Ruby. He is an agile Kanban practitioner who loves to collaborate on development projects.

MEMBER SINCE February 12, 2013