Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

synapsis

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

synapsis

Automatic pub/sub over every possible network. It just works.

  • 0.5.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Synapsis

Inter connect local machines together with a Express like socket router. Data transfer is cipphered with Diffie Hellman algorithm (dynamic public / private key exchange between peers based on a shared password).

http://inkican.com/wp-content/uploads/2016/11/67300035-mesh-wallpapers-1-1024x576.jpg

Usage

Instanciate Peer

Instanciate new peer:

var Synapsis = require('synapsis');

var synapse = new Synapsis({
  namespace: 'namespace-name',
  password: 'long-password-16-random-caracters-recommended',
  // Identity will be shared to all peers
  identity: {
    name : require('os').hostname()
  }
});

// Now start the peer
synapse.start();

Expose Socket Routes

Now expose socket routes that can be triggered by other peers:

synapse.router('command:restart', () => {
  console.log('Got Restart');
});

synapse.router('command:sync_db', (new_db, reply) => {
  fs.writeFile('current_db.json', new_db, (err) => {
     if (err)
       return reply(err);
     reply(null, 'DB Synced');
  });
});

Send/Broadcast Actions to Peers

To send actions to other peers:

// Broadcast a message to all nodes (w/o current) and do not wait for response
synapse.broadcast('command:restart');

// Broadcast a message + data to all nodes (w/o current) and do not wait for response
synapse.broadcast('command:restart', { some : 'data' });

// Broadcast a message to all nodes (w/o current) and receive messages from each (RPC like)
synapse.broadcast('command:sync_db', { my : { db : true } }, function(err, response, identity) {
  console.log(`Err= ${err} Response = ${response} from = ${identity.name}`);
});

Or send to a specific peer:

var peer = synapse.getPeers()[0];

synapse.send(peer.id, 'command:restart');

synapse.send(peer.id, 'command:restart', function(err, data, identity) {
  console.log(err, data, identity);
});

synapse.send(peer.id, 'command:restart', {mu : { db : true} }, function(err, data, identity) {
  console.log(err, data, identity);
});

Event Handling

synapse.on('peer:connected', function(identity, socket) {
  console.log('New peer detected', identity);
});

synapse.on('peer:disconnected', function(identity) {
  console.error(identity);
});

synapse.on('error', function(err) {
  console.error(err);
});

synapse.on('ready', function() {
  console.log('Peer Ready');
});

Test

Checkout test/ folder for more examples/

License

MIT

FAQs

Package last updated on 25 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc