Skip to content
/ netify.js Public

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

License

Notifications You must be signed in to change notification settings

Sxip/netify.js

Repository files navigation


Netify.js


NPM version NPM downloads Neitfy license

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;

About

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

Topics

Resources

License

Stars

Watchers

Forks