protosocket

Hassle-free protobuffer for any type of server or client, TCP, TLS, HTTP or Websockets.

ProtoSocket

Hassle-free protobuffer for any type of server or client, TCP, TLS, HTTPS or HTTP.

Install

npm install protosocket

Endpoints

Node.js provides TCP, TLS, HTTP and HTTPS out-of-the-box, but you can augment ProtoSocket to additional endpoints by adding a library to ProtoSocket.addEndpoint function.

var
    PS = require('protosocket'),
    Server = PS.Server;
 
// adding spdy to the available servers list... 
 
PS.addEndpoint('spdy', {
    lib: require('spdy'),
    clientfunction(create){
        // this context is the require('spdy') 
        return create ? this.createAgent : this.Agent;
    },
    serverfunction(create){
        // this context is the require('spdy') 
        // return the creator, no "new" will be called on these 
        // if you must, provide a bound version of the creator 
        // like 
        // function(options){ 
        //   return this.createServer({ 
        //     cert: fs.readFileSync(options.cert), 
        //     key: fs.readFileSync(options.key) 
        //   }); 
        // } 
        // The second "server" only exists for a instanceof check 
        return create ? this.createServer : this.server.Server;
    }
});
 
var server = new Server('spdy', {
    cert: 'cert.pem',
    key:  'cert.pem'
});
 
server.listen(8080, 'localhost');
 
server.on('connection', function(socket){
    console.log('connected');
});
 
server.on('error', function(errorsocket){
    console.log('error', error);
});
 
server.on('data', function(buffersocket){
    console.log();
});

Server

Common interface for all types of Node.js servers. If another server (Restify, Express, Hapi, etc) uses any of those underlying server, it will work.

server.listen(8080, 'localhost');
 
server.on('connection', function(socket){
    console.log('connected');
});
 
server.on('error', function(errorsocket){
    console.log('error', error);
});
 
server.on('data', function(buffersocket){
    console.log();
});
 
server.on('end', function(socket){
    console.log('disconnected');
});

Client

Information

  • As stated by Google, Protocol Buffers aren't made for large data, so the each packet header are capped to 65kb of payload data

  • You can send as many packets as you want, they will be grouped up and sent. Don't expect packets that should be sent in order , to retain order if you send them manually, prefer to send an array of Protocol Buffer messages instead of sending individual packets. Large data sets should always be sent this way and not mangled in a single Protocol Buffer message (don't be like using a repeated bytes data field for example). (see this Techniques - Protocol Buffers)

  • Each individual Protocol Buffer message have an added header to ensure consistency (that is, 2 CRC32 fields for header and data checksums, protocol magic version, and 16bit length of payload data, that is, the length of Protobuf message itself), that is around 11 bytes overhead per message. Take that in consideration when sending and receiving a lot of small messages.

  • The packet headers are crafted "manually" because protobuf.js throw exceptions when dealing with malformed data and doesn't support "stacking" of individual packets. Also, TCP might send data in chunks, so it's necessary to have a header to ensure that bad data is dropped without interrupting your application. You must also take that in consideration.

TODO

  • Make the client work on the browser out-of-the-box