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

net-request-response

v0.0.14

Published

It implements request-response model for net.socket.

Downloads

32

Readme

Net-Request-Response

It is a node module implements request-response model with for net.socket to make communication quicker and easier. It uses a custom protocol.

How to install

npm install net-request-response

Quick Start

var nrr = require('../index');

//server

var server = nrr.createServer();

server.on('clientConnected', function (client) {
	client.on('onRequest', function (payload, response){
    	console.log('Message from client: '+payload.toString());
    	//response
    	response('Okay, I am server.');
	});
});

server.listen(8080);

//client

var client =new nrr.Client();

client.connect(8080);

client.on('connect', function () {

	//send a string or buffer
	this.send('Hello, I am client.', function (error, responsePayload){
    	if (error) {
        	console.log(error);
        	return;
    	}
    	console.log('Response from server: '+responsePayload.toString());
	});
})

API

Net-Request-Reponse Methods

createServer(options)

options see nodejs document net.createServer.
callback is not supported, use clientConnected event instead.

createClient(options)

options see nodejs document net.createConnection.
callback is not supported, use connect event instead.

Client

new nrr.Client() can also be used to create a new client and then you can invoke client.connect() to connect lately

Server Events

server will emit some events about the state of qtpServer

'ready'

server will emit ready event once the server is listening correctly

server.on('ready',function(){
	console.log(this.address())
});

'clientConnected'

server will emit clientConnected event if a client is connected to this server. A client object will be passed as an argument

server.on('clientConnected',function(client){
	//handler client here
});

'error'

server will emit error event when an error was caught. An error object will be passed as an argument

'close'

Fired when server is closed and stopped from listening

Server Methods

listen(options)

options see nodejs document net.Server.listen. Callback is not supported, use 'ready' event instead.

close()

See nodejs document net.Server.close.

address()

Return an address object

Client Events

client will emit some events when a request or response was handled ####'onRequest' client will emit onRequest event when it receives request from another side of socket with arguments payload and reponse:

  • payload. Buffer.
  • response. A function to call if you want to response this request.

client.on('onRequest',function(payload,response){
	console.log(payload);
	//response with a buffer or string
	response('message received');
});
	

'data'

raw tcp data will be passed to data event

client.on('data',function(data){
	//raw tcp data, including header
	console.log(data);
});

'error'

client will emit error event when an error was caught. An error object will be passed as an argument

'close'

fired when client is closed

Client Methods

send(payload,onResponse)

A request can be sent either on server side or client side

  • payload. Buffer or String to be sent.
  • onResponse. Function, it get invoked with arguments error and payload if a response is received.

client.send('Hello',function(error,payload){
	//get response
	if(error){
		console.log(error);
		return;
	}
		
	//payload is buffer
	console.log(payload);
});

close()

Message Format

  • type: qtp use the first 1 bit to mark request and response: 0 for request and 1 for response

  • sequenceNumber: the next 15 bits is sequenceNumber which is used to distinguish different messages, it is automatically increased by 1 everytime and it will be reset to 0 if it reaches 32768

  • remainingLength: It stands for the length of message body and it is extendible. The first 1 bit of every byte is to mark the end of header (when the bit is 0)

    `00000101`           means length 5
    `10000001 00000010`  means length 129   (1*2^7 + 2)
  • payload: Message body