StiliyanKushev / node-port-reuse-addon

This is a really simple to use linux native node addon that enables port reuse without needing to use clusters in nodejs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node port reuse addon

This is a really simple to use linux native node addon that enables port reuse without needing to use clusters in nodejs.

It works by binding a socket with SO_REUSEPORT and passing it to nodejs as a handle whenever you want to listen on that port.

The native addon is built using dotnet core 8 and it's amazing new AOT feature.

Quick start

Install the package:

npm i node-port-reuse-addon

Example usage:

const http = require('http');
const library = require('node-port-reuse-addon');

// instead of just the port, pass a handle with the already bound port.
const handle = { fd: library.bindPort(12345) };

const server = http.createServer((req, res) => {
    res.end(`Hello HTTP ${id}`);
});

server.listen(handle, () => {
    console.log('HTTP server is listening');
});

Prerequisites

If you want to build from source manually you need:

Build the .node file

make node

Build npm addon

make addon

Run the unit tests

make tests

pm2 proof of concept

As a proof of concept you can run a dummy http server using pm2 but without it's clustering options. (i.e. -i N).

# all of those should keep running :)
pm2 start ./tests/server.js -f
pm2 start ./tests/server.js -f
...
pm2 start ./tests/server.js -f

About

This is a really simple to use linux native node addon that enables port reuse without needing to use clusters in nodejs.


Languages

Language:JavaScript 64.5%Language:C# 30.4%Language:Makefile 5.1%