# tcp-base

> A base class for tcp client with basic functions

Latest version **3.2.0** (published 2023-08-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install tcp-base
pnpm add tcp-base
yarn add tcp-base
bun add tcp-base
```

## 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 | 3.2.0 |
| Published | 2023-08-30 |
| First published | 2016-09-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.0.0 |
| Dependencies | 2 |
| Unpacked size | 16.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | gxcsoccer |
| Maintainers | coderhaoxin, gxcsoccer, shaoshuai0102, fengmk2 |
| Keywords | tcp |

## Links

- npm: https://www.npmjs.com/package/tcp-base
- Repository: https://github.com/node-modules/tcp-base
- Homepage: https://github.com/node-modules/tcp-base#readme
- Issues: https://github.com/node-modules/tcp-base/issues
- npm.io page: https://npm.io/package/tcp-base

## Dependencies (2)

- [sdk-base](https://npm.io/package/sdk-base.md) ^3.1.1
- [is-type-of](https://npm.io/package/is-type-of.md) ^1.0.0

## Recent versions

- 3.2.0 (latest) — 2023-08-30
- 3.1.1 — 2022-09-01
- 3.1.0 — 2017-12-01
- 1.1.2 — 2017-08-16
- 1.1.1 — 2017-08-11
- 3.0.0 — 2017-04-20
- 2.0.0 — 2017-02-17
- 1.1.0 — 2016-11-30
- 1.0.2 — 2016-11-11
- 1.0.1 — 2016-10-20
- 1.0.0 — 2016-09-18

## README

# tcp-base

A base class for tcp client with basic functions

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/node-modules/tcp-base/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/tcp-base/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/tcp-base.svg?style=flat-square
[npm-url]: https://npmjs.org/package/tcp-base
[codecov-image]: https://codecov.io/gh/node-modules/tcp-base/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/tcp-base
[download-image]: https://img.shields.io/npm/dm/tcp-base.svg?style=flat-square
[download-url]: https://npmjs.org/package/tcp-base

## Install

```bash
npm install tcp-base --save
```

Node.js >= 6.0.0 required

## Usage

A quick guide to implement a tcp echo client

Client:

```js
const TCPBase = require('tcp-base');

/**
 * A Simple Protocol:
 *   (4B): request id
 *   (4B): body length
 *   ------------------------------
 *   body data
 */
class Client extends TCPBase {
  getHeader() {
    return this.read(8);
  }

  getBodyLength(header) {
    return header.readInt32BE(4);
  }

  decode(body, header) {
    return {
      id: header.readInt32BE(0),
      data: body,
    };
  }

  // heartbeat packet
  get heartBeatPacket() {
    return Buffer.from([ 255, 255, 255, 255, 0, 0, 0, 0 ]);
  }
}

const client = new Client({
  host: '127.0.0.1',
  port: 8080,
});

const body = Buffer.from('hello');
const data = Buffer.alloc(8 + body.length);
data.writeInt32BE(1, 0);
data.writeInt32BE(body.length, 4);
body.copy(data, 8, 0);

client.send({
  id: 1,
  data,
  timeout: 5000,
}, (err, res) => {
  if (err) {
    console.error(err);
  }
  console.log(res.toString()); // should echo 'hello'
});
```

Server:

```js
'use strict';

const net = require('net');

const server = net.createServer(socket => {
  let header;
  let bodyLen;

  function readPacket() {
    if (bodyLen == null) {
      header = socket.read(8);
      if (!header) {
        return false;
      }
      bodyLen = header.readInt32BE(4);
    }

    if (bodyLen === 0) {
      socket.write(header);
    } else {
      const body = socket.read(bodyLen);
      if (!body) {
        return false;
      }
      socket.write(Buffer.concat([ header, body ]));
    }
    bodyLen = null;
    return true;
  }

  socket.on('readable', () => {
    try {
      let remaining = false;
      do {
        remaining = readPacket();
      }
      while (remaining);
    } catch (err) {
      console.error(err);
    }
  });
});
server.listen(8080);
```

[MIT](LICENSE)

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