npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tcp-bridge

v1.1.3

Published

Server to server(s) tcp bridge with automatic reconnection to each server, the sending queue, the lifetime of the tasks for sending

Downloads

3

Readme

tcp-bridge

Description

This is a small framework for connecting 2 or more servers to each other by tcp.

The framework has the function of automatic reconnection to each server, the sending queue, the lifetime of the tasks for sending.

Installation

With yarn:

yarn add tcp-bridge

or with npm:

npm install tcp-bridge

Usage

For a simple connection of two servers on localhost, you can do this:

const BridgeClass = require('tcp-bridge');
const Bridge = new BridgeClass();
Bridge.addPoint({
    port: 2020
});
Bridge.addPoint({
    port: 3030
});

If possible, connections will be established to servers localhost:2020 and localhost:3030.

All data from server 1 will be sent to server 2. All data from server 2 will be sent to server 1.

Servers may be more

require('tcp-bridge') returns constructor of Bridge class. To create new instance of Bridge you can do this:

const BridgeClass = require('tcp-bridge');
const Bridge = new BridgeClass();

For debugging you can use debug npm package or with same functionality. Example debugging: Install debug package:

yarn add debug

or:

npm install debug

then code:

const BridgeClass = require('tcp-bridge');
const debug = require('debug');
const Bridge = new BridgeClass(debug);

and run script with debug env variable, ex.:

DEBUG=* node index.js

BridgeInstance functions

#addPoint(params) -> point uuid identifier

Adds a new server, initiates a connection and returns a unique identifier.

Signature:

BridgeInstance.addPoint({
    port,
    host,
    reconnectOnClose,
    reconnectOnHasSendData,
    checkTime,
    taskTimeout,
    enabledRedirect,
    encoding,
})

| Parameter | Required | Description | Default value | | - | - | - | - | | port | true | Port of server | | | host | | Hostname (or ip) of server | '127.0.0.1' | | reconnectOnClose | | auto reconnect on close connection | true | | reconnectOnHasSendData | | auto reconnect if connection is closed and clinet has data to send | false | | checkTime | | time to check new data for sending | 300 ms | | taskTimeout | | task lifetime (in ms), 0 for disable | 60000 | | enabledRedirect | | if false - data from this point will not be redirected | true | | encoding | | Set the encoding for the socket as a Readable Stream. See readable.setEncoding() for more information. | null |

#removePoint(identifier)

Disconnects and removes server by identifier.

| Parameter | Required | Description | Default value | | - | - | - | - | | identifier | true | Identifier of point (server) | |

#sendDataToPoint(identifier, data)

Send data to one point.

| Parameter | Required | Description | Default value | | - | - | - | - | | identifier | true | Identifier of point (server) | | | data | true | Bytes (as in net library in socket.send function) | |

#getPoint(identifier) -> point instance

Getting one point instance.

| Parameter | Required | Description | Default value | | - | - | - | - | | identifier | true | Identifier of point (server) | |

Client (Point) functions and properties

#enableRedirect()

Data from this point will be redirected.

#disableRedirect()

Data from this point will not be redirected.

#on('data', callback) – event, triggered if data will be received

#on('ready') – event, triggered if connection is activated

property .connected – true, if connection to server is active

More examples

4 servers with debug and encoding UTF8

const debug = require('debug');
const BridgeClass = require('tcp-bridge');
const Bridge = new BridgeClass(debug);

const firstUid = Bridge.addPoint({
    port: 2020,
    encoding: 'utf8',
});
Bridge.addPoint({
    port: 3030,
    reconnectOnClose: false,
    reconnectOnHasSendData: true,
    encoding: 'utf8',
});
Bridge.addPoint({
    port: 4040,
    encoding: 'utf8',
});
Bridge.addPoint({
    port: 5050,
    encoding: 'utf8',
});

Bridge.removePoint(firstUid);

const lastUid = Bridge.addPoint({
    port: 6060,
    encoding: 'utf8',
});

const point = Bridge.getPoint(lastUid);
point.on('ready', data => {
    console.log('READY');
});
point.on('data', data => {
    console.log('DATA', data);
});