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

nssocket

v0.6.0

Published

An elegant way to define lightweight protocols on-top of TCP/TLS sockets in node.js

Downloads

6,010,465

Readme

Synposis

An elegant way to define lightweight protocols on-top of TCP/TLS sockets in node.js

Motivation

Working within node.js it is very easy to write lightweight network protocols that communicate over TCP or TLS. The definition of such protocols often requires repeated (and tedious) parsing of individual TCP/TLS packets into a message header and some JSON body.

Build Status

Build Status

Installation

  [sudo] npm install nssocket

How it works

With nssocket this tedious bookkeeping work is done automatically for you in two ways:

  1. Leverages wildcard and namespaced events from EventEmitter2
  2. Automatically serializes messages passed to .send() and deserializes messages from data events.
  3. Implements default reconnect logic for potentially faulty connections.
  4. Automatically wraps TCP connections with TLS using a known workaround

Messages

Messages in nssocket are serialized JSON arrays of the following form:

  ["namespace", "to", "event", { "this": "is", "the": "payload" }]

Although this is not as optimal as other message formats (pure binary, msgpack) most of your applications are probably IO-bound, and not by the computation time needed for serialization / deserialization. When working with NsSocket instances, all events are namespaced under data to avoid collision with other events.

Simple Example

  var nssocket = require('nssocket');

  //
  // Create an `nssocket` TCP server
  //
  var server = nssocket.createServer(function (socket) {
    //
    // Here `socket` will be an instance of `nssocket.NsSocket`.
    //
    socket.send(['you', 'there']);
    socket.data(['iam', 'here'], function (data) {
      //
      // Good! The socket speaks our language 
      // (i.e. simple 'you::there', 'iam::here' protocol)
      //
      // { iam: true, indeedHere: true }
      //
      console.dir(data);
    })
  });
  
  //
  // Tell the server to listen on port `6785` and then connect to it
  // using another NsSocket instance.
  //
  server.listen(6785);
  
  var outbound = new nssocket.NsSocket();
  outbound.data(['you', 'there'], function () {
    outbound.send(['iam', 'here'], { iam: true, indeedHere: true });
  });
  
  outbound.connect(6785);

Reconnect Example

nssocket exposes simple options for enabling reconnection of the underlying socket. By default, these options are disabled. Lets look at a simple example:

  var net = require('net'),
      nssocket = require('nssocket');
  
  net.createServer(function (socket) {
    //
    // Close the underlying socket after `1000ms`
    //
    setTimeout(function () {
      socket.destroy();
    }, 1000);
  }).listen(8345);
  
  //
  // Create an NsSocket instance with reconnect enabled
  //
  var socket = new nssocket.NsSocket({
    reconnect: true,
    type: 'tcp4',
  });
  
  socket.on('start', function () {
    //
    // The socket will emit this event periodically
    // as it attempts to reconnect
    //
    console.dir('start');
  });
  
  socket.connect(8345);

API

socket.send(event, data)

Writes data to the socket with the specified event, on the receiving end it will look like: JSON.stringify([event, data]).

socket.on(event, callback)

Equivalent to the underlying .addListener() or .on() function on the underlying socket except that it will permit all EventEmitter2 wildcards and namespaces.

socket.data(event, callback)

Helper function for performing shorthand listeners namespaced under the data event. For example:

  //
  // These two statements are equivalent
  //
  someSocket.on(['data', 'some', 'event'], function (data) { });
  someSocket.data(['some', 'event'], function (data) { });

socket.end()

Closes the current socket, emits close event, possibly also error

socket.destroy()

Remove all listeners, destroys socket, clears buffer. It is recommended that you use socket.end().

Tests

All tests are written with vows and should be run through npm:

  $ npm test

Author: Nodejitsu

Contributors: Paolo Fragomeni, Charlie Robbins, Jameson Lee, Gene Diaz Jr.