Notice: This library is Outdated and haven’t been updated for a long time. Fork the repository and update as per your own need.
If you are a NodeJS application developer, you are developing it in a single-threaded/single-process-based manner by default. If you deploy it on a multi-CPU/core-based server, you are only under-utilizing it. Sure, we can do better, right? Nodejs also provides some facilities to improve its default single-thread nature through its cluster module. Sure, you will have to know how to code that way. But, wouldn’t it be nice if you tell it to run your application in the multi-cluster way, and it just works in that way, without manually coding a bunch of lines?
I am happy to introduce you to a new NodeJS open-source library that I have created recently, which will facilitate precisely that: making your NodeJS server/application in multi-process/clustered mode in a super easy way, without knowing much inner details of how clustering works. The library is available as an npm package, and the source is available on GitHub:
NPM Package: https://www.npmjs.org/package/clustered-node
Source On Github: https://github.com/ranacseruet/clustered-node
I was working on a small proxy server at my job that needed to be multi-process-based. I also found an existing npm module named “multi-node,” but it wasn’t maintained anymore and was not compatible with the latest Node.js versions. I wasn’t able to find anything else closely similar to its alternatives.
Thus, we implemented our multi-process mechanism with the help of the Hipache source code. Later, I thought of having it as a separate module as a multi-node, so I created its npm package and open-sourced it on GitHub.
Based on the reputation of the earlier-mentioned multi-node library, it’s obvious that developers liked the idea of having the multi-process handling logic separated and using it as a ready solution instead of implementing it individually for all applications. So, I hope this can help you as an alternative to that library.
This library provides a very easy-to-use interface with two simple functions, requiring almost no additional effort to migrate an existing application to multi-process mode.
Additionally, to have your application in multi-clustered mode, you will have to design and develop it as stateless, which is very beneficial for scaling. It will work seamlessly even if you deploy your application in different geographical locations.
It is very easy to install the package as a simple command below:
$npm install clustered-node --save
Code language: PHP (php)
The above command
Currently, this library offers two different use cases:
Multi-Process Server: If your application is a NodeJS server, you can easily pass the server object with the proper host/port where it should listen and a number of processes to this module’s ‘listen’ method. So, you don’t have to use the server object’s internal ‘listen’ method. Example:
var server = http.createServer(someCallbackMethod);
require("clustered-node").listen({port:1337, workers:3}, server);Code language: JavaScript (javascript) Multi-Process App: if your application is a non-server app, like a crawler, test tool, etc, then you can pass your entry point method as the parameter, along with the number of processes to this module’s ‘run’ method. Which will be, in turn, called for every process created. Example:
function runMyApp() {
//start your application coding starts here
}
require("clustered-node").run(config, runServer);Code language: JavaScript (javascript) Though, as shown above, migrating your existing app to a multi-process system is very easy and efficient, there is something you should remember and be careful about your application before you migrate to make sure that the migration goes seamlessly without any issues:
This project is in a very early stage, and I already have several things in mind for improvement on this project:
You are welcome to contribute and provide feedback on this project. To submit suggestions or bug reports, use the GitHub issue tracker for clustered-node.
Pull requests are also very much welcome. This is just an early-stage implementation, and I believe it can be enhanced/improved in many ways other than what I mentioned above. If you are interested in working on any such parts, feel free to start discussing them on GitHub or contact me.
Let me know if I have missed anything or if you like to know about it. Happy coding!
Ever wondered what happens when you run Python code? The Python runtime environment—comprising the interpreter, virtual machine, and system resources—executes your code through bytecode compilation…
Tired of repetitive tasks eating up your time? Python can help you automate the boring stuff — from organizing files to scraping websites and sending…
Learn python file handling from scratch! This comprehensive guide walks you through reading, writing, and managing files in Python with real-world examples, troubleshooting tips, and…
This website uses cookies.