# netify.js

> Netify is a Node.js module that allows you to easily create a TCP server and client.

Latest version **0.0.14** (published 2021-02-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install netify.js
pnpm add netify.js
yarn add netify.js
bun add netify.js
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.14 |
| Published | 2021-02-17 |
| First published | 2019-08-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 46.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Sxip |
| Maintainers | sxips |
| Keywords | node, server, client |

## Links

- npm: https://www.npmjs.com/package/netify.js
- Repository: https://github.com/Sxip/netify.js
- Homepage: https://github.com/Sxip/netify.js#readme
- Issues: https://github.com/Sxip/netify.js/issues
- npm.io page: https://npm.io/package/netify.js

## Dependencies (1)

- [@types/node](https://npm.io/package/@types/node.md) ^12.7.1

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.0.14 (latest) — 2021-02-17
- 0.0.1 (alpha) — 2019-08-14
- 0.0.13 — 2019-12-18
- 0.0.12 — 2019-12-06
- 0.0.11 — 2019-11-25
- 0.0.10 — 2019-10-14
- 0.0.9 — 2019-10-13
- 0.0.8 — 2019-08-28
- 0.0.7 — 2019-08-28
- 0.0.6 — 2019-08-28
- 0.0.5 — 2019-08-15
- 0.0.4 — 2019-08-14
- 0.0.3 — 2019-08-14
- 0.0.2 — 2019-08-14

## README

<div align="center">
  <br />
  <p>
    <a href="#"><img src="https://raw.githubusercontent.com/Sxip/netify.js/master/assets/Netify.png" width="150" alt="Netify.js" /></a>
  </p>
  <br />
  <p>
    <a href="https://www.npmjs.com/package/netify.js"><img src="https://img.shields.io/npm/v/netify.js.svg?style=flat&color=brightgreen" alt="NPM version" /></a>
    <a href="https://www.npmjs.com/package/netify.js"><img src="https://img.shields.io/npm/dt/netify.js" alt="NPM downloads" /></a>
    <a href="#"><img src="https://img.shields.io/npm/l/netify.js" alt="Neitfy license" /></a>
  </p>
</div>

## About

<b>Netify</b> is a [Node.js](https://nodejs.org/) module that allows you to easily create a TCP server and client.

- ⚙️ Create your own protocol
- ⛓️ Promise based
- ⚡️ Performant

## Getting Started

### Installation

```bash
npm i netify.js
# or yarn add netify.js
```

### Usage

Note: Before creating a server or client, you must define your own protocol or use a premade one.

**Example** - creating a netify server

```ts
const { NetifyServer, NullProtocol } = require('netify.js');

(async () => {
  const netify = new NetifyServer({
    port: 8080,
  }).useProtocol(NullProtocol);

  netify.on('connection', async connection => {
    console.info(`New connection!`);

    connection.write('Hello, client!');
    connection.write('\x00');

    // Sends to the connection
    await connection.flush();

    connection.on('received', message => {
      console.info(`Recieved ${message}`);
    });

    connection.on('close', () => {
      console.warn(`Connection closed!`);
    });
  });

  await netify.serve();
  console.log('Listening!');
})();
```
**Example** - creating a netify client

```js
const { NetifyClient, NullProtocol } = require('netify.js');

(async () => {
  const netify = new NetifyClient({
    host: '127.0.0.1',
    port: 8080,
  }).useProtocol(NullProtocol);

  netify.on('received', message => {
    console.info(`Recieved ${message}`);
  });

  netify.on('close', () => {
    console.warn(`Connection closed!`);
  });

  await netify.connect();
  console.log('Connected!');

  netify.write('Hello, server!');
  netify.write('\x00');

  // Sends to the server
  await netify.flush();
})();
```

### Protocol

**Netify** comes with the following premade protocols.

* Delimiter  
  Used for delimiter based data

* Null  
  Used for data ending with `\x00`

* Chunk  
  Used to handle data chunks

If the premade procols do not fit your needs, you have the option to extend the protocol `Base` class and create your own protocol to fit your needs.

**Example** - Creating your own protocol class

```js
const { Protocol } = require('netify.js');

class class ExampleProtocol extends Protocol {
  constructor({ readBufferSize = 200, writeBufferSize = 200 } = {}) {
    super({
      readBufferSize,
      writeBufferSize,
    });
  }

  /**
   * Handle incoming buffer data
   */
  chunk(bytes) {
    
    // Emits the data to the connection
    this.push('Hello, World!');
  }
}

module.exports = ExampleProtocol;
```

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