🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

cluster-vir

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cluster-vir

Makes it easy to distribute arbitrary scripts across multiple workers.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

cluster-vir

Simplifies running a script across multiple workers. This only works in Node.js (not in the browser) because it relies on Node.js's built-in cluster module.

Install

npm i cluster-vir

Usage

See full docs here: https://electrovir.github.io/cluster-vir

The main export from this package is runInCluster. Pass it a callback to be executed on each worker. Here's an example of how to use it:

import {createServer} from 'node:http';
import {runInCluster} from 'cluster-vir';

/**
 * In the primary thread the output of `runInCluster` gives you an instance of `ClusterManager`
 * which can be used to access all child nodes. In the worker threads it gives you an instance of
 * `WorkerRunner` which can be used to access the worker.
 */
const runner = runInCluster(
    ({worker}) => {
        /**
         * Do the worker actions here. In this example, each worker is listening to a port as an
         * HTTP server.
         */

        /**
         * Cluster workers can share a TCP connection, so an HTTP server can be handled by (and will
         * be load-balanced between) multiple workers (or threads).
         */
        const server = createServer((request, response) => {
            response.writeHead(200);
            response.end(`hello there ${worker.process.pid}`);
        }).listen(8000);

        return () => {
            /**
             * Do cleanup work in here. In this case, we're closing the HTTP server. Note that a
             * cleanup callback cannot be async.
             */
            server.close();
        };
    },
    {
        /**
         * Indicates that all child workers should be started immediately. If set to `false`, you
         * must call `runner.startWorkers()` to start them, giving you time to attach any listeners
         * you may need.
         */
        startWorkersImmediately: true,
    },
);

Notes

  • Workers will not terminate themselves when the callback you provide to runInCluster finishes, you must manually kill them with worker.kill() or process.exit().
  • When all workers have terminated and respawnWorkers has not been set to true, the cluster manager will automatically terminate itself as well.
  • The cluster manager and its workers send messages back and forth so if you add your own event listener to the spawned workers, you'll need an if or switch to focus on your messages.

Keywords

cluster

FAQs

Package last updated on 02 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts