node package manager


socket pooling


Socket pooling in node.js

The problem with socket pooling is there are many use cases. Which is why you mainly see socket pooling libraries for specific protocols (http, mongo, mysql, etc.).

This library neither tries to be too high level (easier but assumes too much), or too low level (flexibility but managing your socket pool becomes hard).

This is for the mid-level.

var Pool = require('socket-pool');
var pool = new Pool([
  { host: '', port: 80, weight: 5 },
  { host: '', port: 80, weight: 10 }
], {
  min: 5,
  max: 20
var do_stuff_with_socket = function(socket) {
  socket.on('data', function(data) {
    // do stuff with data 
  socket.write('some data');
var socket = pool.acquire();
// acquire is 'sync', you either get back a socket or you don't 
if (socket) {
} else {
  // if there's no available socket...  
  // we can queue and let the pool 
  // call it whenever a socket is available 
  // Or we can create our own socket for now... 
  socket = new Socket();
  .... do some stuff with the socket ...
  // when we're done we can give it to the pool too