pnode

peer-to-peer dnode over anything!

Usage no npm install needed!

<script type="module">
  import pnode from 'https://cdn.skypack.dev/pnode';
</script>

README

In progress - API Unstable!


pnode

peer-to-peer dnode over anything!

NPM version

Node tests:

Build Status

Browser tests:

browser support

Summary

pnode is a Node.js library, built ontop of dnode (What's dnode?), allows applications (node and browser) to easily communicate in a peer-to-peer fashion. Since there's no centralised server, there's no single point of failure, this ability simplifies the implementation of resilient applications by adding redundancy.

Features

  • Simplified dnode API
  • Autoreconnects and buffering like upnode
  • Easily utilise different transports
  • Usable in the browser with the Websockets transport
  • Create your own transport types with any duplex stream

Future Features

  • Client function call timeouts
  • Integration with cluster
  • Peer-to-Peer API
    • Each instance is a server and many clients
  • Authentication
    • Password
    • Certificates
    • ACL
  • Proxying RPC
    • Allowing client can communicate with server2 via server1 (clientserver1server2)
    • Achieved by expose()ing another server/client
  • WebRTC transport rtc:// to provide clientclient networks

Download

<codeBlock("npm install " + name)>

npm install pnode

Note: Only node v0.10.x is currently supported

Basic Usage

Server: <showFile("example/basic/https/server.js")>

var pnode = require('../../../');
var server = pnode.server();

server.expose({
  say: function(date) {
    console.log('client says ' + date);
  }
});

server.bind('https://0.0.0.0:8000', function(){
  console.log('bound to all interfaces on port 8000');
});

Client: <showFile("example/basic/https/client.js")>

var pnode = require('../../../');
var client = pnode.client();

client.bind('https://localhost:8000');

client.server(function(remote) {
  remote.say(new Date());
});

You can use a different transport by simply changing the URI. Currently, the following transports are avaiable:

  • tcp
  • tls
  • http
  • https
  • ipc (unix sockets)
  • ws (websockets)

See basic examples

Browser Usage

See browser examples

See this demo ('long-polling-heroku' in the examples), since Heroku doesn't support Websockets, sockjs falls back to XHR long polling with shoe maintaining stream-like behaviour.

API

See API docs

Advanced Usage

server.handle

Instead of server.listen(), you can provide the streams to server.handle():

// handle a read and write stream
foo(function(req, res) {
  server.handle(req, res);
});
// OR
// handle a duplex stream
bar(function(stream) {
  server.handle(stream);
});

Each call to server.handle() will be seen as a new client by the server.

client.createConnection

Instead of client.connect(), you can provide a function which will asynchronously create connection streams:

// create a read and a write stream
client.createConnection(function(readCallback, writeCallback) {
  readCallback(foo.createReadStream());
  writeCallback(bar.createWriteStream());
});
// OR
// create a duplex stream
client.createConnection(function(streamCallback) {
  streamCallback(bar.createStream());
});

This will get called to whenever pnode needs to restablish a connection.

<license()>

MIT License

Copyright © 2013 Jaime Pillora <dev@jpillora.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.