0.0.14 • Published 3 years ago

netify.js v0.0.14

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

About

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

  • ⚙️ Create your own protocol
  • ⛓️ Promise based
  • ⚡️ Performant

Getting Started

Installation

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

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

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

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;
0.0.14

3 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago