net-buffer

send and receive buffer through network socket

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
net-buffer
0.1.07 years ago8 years agoMinified + gzip package size for net-buffer in KB

Readme

net-buffer
Very fast message length-prefixing encoder and decoder.

.encode(Buffer)

encode can accept Buffer or undefined
var net= require('net')
var netBuf= require('net-buffer')

var client= net.connect({port: 8181}, function()
{
    client.write( netBuf.encode(undefined) ) // or just .encode()
    client.write( netBuf.encode( new Buffer('Hello World!') ) )
})

.decode(socket, callback, bodyOnly)

decode directly reads from the socekt without using the socket.on('data', ) event.
var net= require('net')
var decode= require('net-buffer').decode

net.createServer(function(socket)
{
	socket.on('readable', decode(socket, function(buffer){
	    console.log(buffer)
	}, true))
}).listen(8181)
  • socket Socket
  • callback Function, called with a single Buffer argument
  • bodyOnly Boolean, Optional, Default true; Set to false to get body prefixed with length (can reuse Buffer when need to proxy the Buffer)

.setMaxMessageLength(number)

Defines the maximum supported message length (header), valid values are 2 (for 65,135 bytes) and 4 (for 4,294,967,196 bytes), default is 2.
var netBuf= require('net-buffer')
netBuf.setMaxMessageLength(4)

Benchmark

Below is Mac Air 2013 results
$ node test.js 100000
$ > ok sent and received 100000 messages in 0.786218543 seconds

Installation

npm install net-buffer
--- MIT License