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

naked-websocket

v0.1.7

Published

Fastest WebSockets for node to node data exchange.

Downloads

33

Readme

Naked WebSocket

build status NPM version node version

Fastest WebSockets for node to node data exchange.

Connect your back-end node.js applications with WebSockets and exchange data with any format you wish, JSON, MsgPack, none, etc.

Why not just use socket.io or ws modules?

You should use socket.io or ws for Browser applications and Naked WebSocket for back-end applications. Naked WebSocket is a much faster communication link because it uses the raw net or tls sockets, without Browser regard.

Naked WebSocket allows you to use any data framing (message exchange) you wish, for example JSON, MsgPack, SMP, AMP, none, or any other.

Why not just use plain old net or tls without WebSockets?

Because Naked WebSocket gives you a way to connect remote node.js applications via the WebSocket Protocol, while still using the net or tls sockets, this is the best of both worlds! You get:

  • Firewall friendly access.
  • Basic Authentication.
  • HTTP Headers.
  • Aggree on a message exchange format EG: JSON, MsgPack, SMP, AMP, etc.
  • Persistent bidirectional remote node communication.
  • Full control over the raw net or tls sockets.

...

Complies with WebSocket Protocol version 13 as Sub Protocol: 'nws/' + options.version, (you choose own data framing). This solution is not for Browser clients, but for common nodes using this module for data exchange communication.

Installation

npm install naked-websocket

Example

See examples folder.

Server example

const nws = require('naked-websocket');

// use same options as: https://nodejs.org/api/net.html
var options = {
  protocol: 'ws'
};

var server = nws.createServer(options, function(socket) {
  // can examine: socket.headers
  
  console.log('client connected');

  socket.on('data', function(chunk) {
    console.log(chunk.toString());
  });

  socket.write('hello client via net socket');
});

server.listen(8080, function() {
  console.log('server bound');
});

Client example

const nws = require('naked-websocket');

// use same options as: https://nodejs.org/api/net.html
var options = {
  protocol: 'ws',
  hostname: '127.0.0.1',
  port: 8080,
  path: '/foo/bar/?hello=world'
};

var client = nws.connect(options, function(socket) {
  console.log('connected to server!');

  socket.on('data', function(chunk) {
    console.log(chunk.toString());
  });
  
  if ( socket.body ) {    // if server body was trailing connection header, emit.
    socket.emit('data', socket.body);
  }
  
  socket.write('world!');
});

Secure example

Server

const nws = require('naked-websocket');
const fs = require('fs');

// use same options as: https://nodejs.org/api/tls.html, you need to generate own key.pem and cert.pem.
var options = {
  protocol: 'wss',
  slowHandshake: true,    // so can do own auth.
  key: fs.readFileSync(__dirname + '/keys/key.pem'),
  cert: fs.readFileSync(__dirname + '/keys/cert.pem'),
  rejectUnauthorized: false,
  requestCert: true
};

var server = nws.createServer(options, function(socket) {
  // examine: socket.headers.authorization
  if ( !socket.headers.authorization ) {
    socket.goodbye(401);
  } else if ( socket.headers.authorization.password === 'password' ) {
    socket.handshake();
  } else {
    socket.goodbye(401);
  }

  console.log('client connected');
  
  socket.on('data', function(chunk) {
    console.log(chunk.toString());
  });
  
  socket.write('hello client via tls socket');
});

server.listen(8443, function() {
  console.log('server bound');
});

Client

const nws = require('naked-websocket');
const fs = require('fs');

// use same options as: https://nodejs.org/api/tls.html, you need to generate key.pem and cert.pem.
var options = {
  protocol: 'wss',
  hostname: '127.0.0.1',
  port: 8443,
  key: fs.readFileSync(__dirname + '/keys/key.pem'),
  cert: fs.readFileSync(__dirname + '/keys/cert.pem'),
  rejectUnauthorized: false,
  requestCert: true,
  auth: 'username:password'
};

var client = nws.connect(options, function(socket) {
  console.log('connected to server!');
  
  socket.on('data', function(chunk) {
    console.log(chunk.toString());
  });
  
  if ( socket.body ) {    // if server body was trailing connection header, emit.
    socket.emit('data', socket.body);
  }
  
  socket.write('world!');
});

Message framing

Naked WebSocket does not frame messages, it leaves this entirely up to each node. Nodes should deploy their own framing technique, could use JSON, MsgPack, SMP, AMP, or your own.

Messaging using MsgPack (npm install msgpack) Example

const nws = require('naked-websocket');
const msgpack = require('msgpack');    // npm install msgpack

var server = nws.createServer({protocol: 'ws'}, function(socket) {

  var ms = new msgpack.Stream(socket);
  ms.addListener('msg', function(m) {
    console.log('server received message: ', m);
  });

  var payload = {foo : 'bar', num : 101, 'list-of' : [1, 2, 3], buf: new Buffer('hello')};
  var msg = msgpack.pack(payload);
  socket.write(msg);
  
}).listen(8888);


var options = {
  protocol: 'ws',
  hostname: '127.0.0.1',
  port: 8888
};

var client = nws.connect(options, function(socket) {
  
  if ( socket.body ) {    // if server body was trailing connection header, emit.
    socket.emit('data', socket.body);
  }
   
  var ms = new msgpack.Stream(socket);
  ms.addListener('msg', function(m) {
    console.log('client received message: ', m);
  }); 
  
  var payload = {hello: 'from client'};
  var msg = msgpack.pack(payload);
  socket.write(msg);  
  
});

Options

Can use same as: https://nodejs.org/api/net.html (protocol: 'ws') or https://nodejs.org/api/tls.html (protocol: 'wss').

     maxbuffer: 4000,          // max header size, 4000 = 4 KB.
       version: '0.0.1',       // must be same on all peers.
      protocol: 'ws',          // 'wss' = secure (TLS), must be same on all peers.
 slowHandshake: false,         // true: if you wish to manage own auth at app level.
      timedout: 15000,         // how long to wait for connection, 15 seconds.
       noDelay: false          // true = turn nagle batching algorithm off.

Can set own custom headers.

Server example

var server = nws.createServer(options, function(socket) {
  
  socket.handshake({headers: {Framing: 'msgpack', 'X-foo': 'bar'}});
  ...

Client example

var options = {
  protocol: 'ws',
  hostname: '127.0.0.1',
  port: 8443,
  headers: {
    Framing: 'msgpack',
    'X-Hello': 'World'
  } 
};

var client = nws.connect(options, function(socket) {
  ...

License

Choose either: MIT or Apache 2.0.