GithubHelp home page GithubHelp logo

msfidelis / topper Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 1.0 144 KB

Lightweight Node.js framework to build fast microservices on TCP Servers. :electric_plug: :zap: :battery:

JavaScript 100.00%
tcp-server microservices framework nodejs scale microseconds tcp net socket

topper's Introduction

Architecture Benchmark

Benchmark Tool

Benchmark Tool: Autocannon HTTP/1.1 benchmarking tool

autocannon -c 20 -a 5000  -m POST --body '{"numbers": [1, 2, 3, 4, 5, 6, 7, 8, 9]}' --headers 'Content-Type:Application/json' http://localhost:9000/sum

REST to REST

  • Express API Endpoint --> Express REST Microservice
Stat         Avg    Stdev Max
Latency (ms) 76.52  22.53 234.85
Req/Sec      250    50.99 320
Bytes/Sec    112 kB 23 kB 144 kB

5k requests in 20s, 2.25 MB read

REST to Topper

  • Express API Endpoint --> Topper TCP Microservice
Stat         Avg    Stdev  Max
Latency (ms) 24.16  6.4    98.45
Req/Sec      714.29 244.42 919
Bytes/Sec    312 kB 107 kB 402 kB

5k requests in 7s, 2.19 MB read

Installation

npm install topper

Create a simple TCP Microservice

  • Write a simple functions to receive an TCP Payload, and a server socket context and register on a Topper Server instance.
const { Server } = require('topper');
const server = new Server('0.0.0.0', 4000);

const task = async (data, socket) => {
    console.log(`New message!`);
    await socket.write(`Message payload: ${data}`);
}

server.task(task);

Client Console

nc localhost 4000
ห†
verything is connected, Todd...
Message payload: verything is connected, Todd...

Server console output

node app.js
New task registered on tcp://localhost:4000
New message!

Send an JSON message to your TCP Microservice

  • Write a function to receive an JSON string and execute some actions with payload
const { Server } = require('topper');
const server = new Server('0.0.0.0', 4000);

const task = async (data, socket) => {
    socket.write(`hello, my name is ${data.name} and i'm ${data.age} old\n`);
}

server.task(task);
nc localhost 4000
{"name": "Matheus Fidelis", "age":22}
hello, my name is Matheus Fidelis and i'm 22 old

Create a Multi Task Microservice

Create some arrays with task definitions and register on your server

const { Server } = require('./');
const server = new Server('0.0.0.0', 4000);


const tasks = [
    {
        name: 'Ping',
        task: async (data, socket) => await socket.write('Pong\n')
    },
    {
        name: 'Sum',
        task: async (data, socket) => {
            let sum = data.numbers.reduce((prev, curr) => prev + curr);
            await socket.write(`The sum is: ${sum} \n`);
        }
    }
]

server.multiTask(tasks);

Client console

echo 'Sum {"numbers": [1, 2, 3, 4, 5]}' | nc localhost 4000
echo 'Ping' | nc localhost 4000

Output:

The sum is: 15
Pong

Create a simple client

const { Client } = require('topper');
const client = new Client();

client.addServer('0.0.0.0', 4000);

client.send('Sum', {numbers: [1,2]})
    .then(success => console.log(success.toString()));

topper's People

Contributors

dependabot[bot] avatar msfidelis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

andrentfs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.