# socket-pool

> Node socket pool for persistent TCP connections

Latest version **1.2.3** (published 2018-06-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install socket-pool
pnpm add socket-pool
yarn add socket-pool
bun add socket-pool
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.3 |
| Published | 2018-06-07 |
| First published | 2016-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | 6 - 9 \|\| >= 10.3 |
| Dependencies | 2 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | kaelzhang |
| Maintainers | kael |
| Keywords | socket-pool, pool, connection-pool, persistent |

## Links

- npm: https://www.npmjs.com/package/socket-pool
- Repository: https://github.com/kaelzhang/node-socket-pool
- Homepage: https://github.com/kaelzhang/node-socket-pool#readme
- Issues: https://github.com/kaelzhang/node-socket-pool/issues
- npm.io page: https://npm.io/package/socket-pool

## Dependencies (2)

- [p-timeout](https://npm.io/package/p-timeout.md) ^2.0.1
- [generic-pool](https://npm.io/package/generic-pool.md) ^3.4.2

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.2.3 (latest) — 2018-06-07
- 1.2.2 — 2018-05-17
- 1.2.1 — 2017-10-16
- 1.2.0 — 2017-09-21
- 1.1.0 — 2017-09-21
- 1.0.3 — 2017-08-31
- 1.0.2 — 2017-08-03
- 1.0.1 — 2017-08-03
- 0.0.1-security — 2016-06-23

## README

[![Build Status](https://travis-ci.org/kaelzhang/node-socket-pool.svg?branch=master)](https://travis-ci.org/kaelzhang/node-socket-pool)
<!-- optional appveyor tst
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/kaelzhang/node-socket-pool?branch=master&svg=true)](https://ci.appveyor.com/project/kaelzhang/node-socket-pool)
-->
<!-- optional npm version
[![NPM version](https://badge.fury.io/js/socket-pool.svg)](http://badge.fury.io/js/socket-pool)
-->
<!-- optional npm downloads
[![npm module downloads per month](http://img.shields.io/npm/dm/socket-pool.svg)](https://www.npmjs.org/package/socket-pool)
-->
<!-- optional dependency status
[![Dependency Status](https://david-dm.org/kaelzhang/node-socket-pool.svg)](https://david-dm.org/kaelzhang/node-socket-pool)
-->

# socket-pool

Node socket pool for persistent TCP/IPC connections

## Install

```sh
$ npm install socket-pool --save
```

## Usage

```js
import Pool from 'socket-pool'

const pool = new Pool({
  connect: {
    host: 'localhost',
    port: 2181
  },

  // Defaults to `3000`
  connectTimeout: 3000,

  pool: {
    // the options of generic-pool
    max: 100,
    min: 10
  }

  // other options of net.Socket
})

pool.acquire()
.then(socket => {
  socket.on('data', chunk => {
    // concat chunks

    // To re-use TCP connections, it is better NOT to end or destroy a socket
    // after data received.
    // Some mechanism could be used to tell the client if there is no more
    // chunks, such as:
    // - design a protocol to define the content-length of the incoming chunks.
    if (dataFinished) {
      // Release the socket resource,
      // then it can be required again.
      socket.release()
    }
  })
})

// And then, the usage of `socket` is nearly the same as `new net.Socket`
```

## new Pool({connect, pool, ...socketOptions})

- **pool** `Object` the options of [`generic-pool`](https://www.npmjs.com/package/generic-pool), and the value is simply passed
- **connectTimeout** `Number=3000` the milliseconds socket pool will wait for a socket to connect to the server before timing out. Defaults to `3000` milliseconds.
- **socketOptions** `Object` the options of `new net.Socket(options)` of the vanilla node.js. The only difference is that the option `socketOptions.allowHalfOpen` defaults to `true`. If half-opened TCP connections are not allowed, `allowHalfOpen` should be explicit set to `false`. But setting this to `false` is kind of silly, since that's the whole purpose of this lib.

### connect `Object`

If `connect.path` is specified, then other socket options will be ignored, and it is only for IPC connections.

- **path** `String` the same argument of `socket.connect(path)` of the vanilla node.js

Otherwise, it is for TCP connections, available options are:

- **port**
- **host**
- **localAddress**
- **localPort**
- **family**
- **hints**
- **lookup**

## pool.acquire()

Returns `Promise`.

- `Promise.resolve(socket)` If the socket is successful connected
- `Promise.reject(error)` If there are any errors
  - **error** `SocketError|TimeoutError`

```js
import {
  // If connectTimeout is specified and timed out to connect to server
  TimeoutError,
  // Socket error
  SocketError
} from 'socket-pool'

pool.acquire()
.then(
  socket => {
    // do something with socket
  },

  error => {
    console.log(error instanceof SocketError || error instanceof TimeoutError)
    // true
  }
)
```

The acquired socket is a wrapped `net.Socket` instance which will be destroyed when `'end'` event occurs, and some additional methods are available:

### socket.release()

The `socket-pool`-specified method to release the socket to the pool

### socket.destroy()

Destroy the socket instance.

## License

MIT

---
_Source: https://npm.io/package/socket-pool · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
