Nossock is a small lib for implementing lightweight protocols on top of TCP & TLS.
- Fast: serializes messages to JSON, but sends Buffer objects as it is with no overhead
- Lower memory consumption: one reusable buffer for parsing incoming messages
- TCP and TLS: easy configurable
$ npm install nossockSee losin lib that adds extra functionality to nossock.
var nossock = require('nossock');
/* create server */
nossock.createServer('tcp', {}, function(socket) {
socket.on('hello', function(body) {
console.log('On server - hello', body);
socket.send('bye', 'cruel world');
});
}).listen(8797);
/* create client */
nossock.createClient('tcp', {port: 8797}, function(socket) {
socket.on('bye', function(body) {
console.log('On client - bye', body);
});
socket.send('hello', 'world');
});var fs = require('fs'),
nossock = require('nossock');
/* create server */
var serverOpts = {
cert: fs.readFileSync('/path/to/server.crt'),
key: fs.readFileSync('/path/to/server.key'),
ca: fs.readFileSync('/path/to/ca.crt'),
passphrase: 'passphrase',
requestCert: true,
rejectUnauthorized: false
};
nossock.createServer('tls', serverOpts, function(socket) {
console.log('Got connection from', socket.socket.remoteAddress);
console.log('Certificate', socket.socket.getPeerCertificate());
socket.on('hello', function(body) {
console.log('On server - hello', body);
socket.send('bye', 'cruel world');
});
}).listen(8797);
/* create client */
var clientOpts = {
host: 'localhost',
port: 8797,
cert: fs.readFileSync('/path/to/client.crt'),
key: fs.readFileSync('/path/to/client.key'),
ca: fs.readFileSync('/path/to/ca.crt'),
passphrase: 'passphrase'
};
nossock.createClient('tls', clientOpts, function(socket) {
socket.on('bye', function(body) {
console.log('On client - bye', body);
});
socket.send('hello', 'world');
});For more examples, see tests
Returns:
server: server instance
Arguments:
type:'tcp'(default) |'tls'options: options object for underlying tcp or tlscreateServerfunctioncallback: connection listener
Returns:
socket: Nossock instance
Arguments:
type:'tcp'(default) |'tls'options: options object for underlying tcp or tlsconnectfunctioncallback: connection listener
socket.send(name, obj)- send message with namenameand objectobj. obj will be serialized in JSON. If obj is instance of Buffer, it won't be serialized and will be sent as it is, with no extra overhead.socket.on(name, callback)- subscribe onnameevent. Once got,callbackwill be called with received object as the only parameter.namecould be anything except of the reserved ones (likeerrororend).socket.socket- underlying socket. Could be used to retreive some useful info such assocket.socket.remoteAddress' orsocket.socket.getPeerCertificate()(for TLS connections).
connect, end, timeout, error, close
$ sudo npm install nodeunit -g
$ npm testDue to message structure, message body size is limited to 4096 Mb. No idea why
you'll want to send such a big message, in any case it worth to split it to
lots of smaller parts.
- Igor Sosyura (help & bugfix)
- Evgeniy Agafonov (original idea)
MIT
