Naked WebSocket
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 = ; // use same options as: https://nodejs.org/api/net.htmlvar options = protocol: 'ws'; var server = nws; server;
Client example
const nws = ; // use same options as: https://nodejs.org/api/net.htmlvar options = protocol: 'ws' hostname: '127.0.0.1' port: 8080 path: '/foo/bar/?hello=world'; var client = nws;
Secure example
Server
const nws = ;const 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 cert: fs rejectUnauthorized: false requestCert: true; var server = nws; server;
Client
const nws = ;const 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 cert: fs rejectUnauthorized: false requestCert: true auth: 'username:password'; var client = nws;
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 = ;const msgpack = ; // npm install msgpack var server = nws; var options = protocol: 'ws' hostname: '127.0.0.1' port: 8888; var client = nws;
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.