Skip to content

Node.js port of Zyre - an open-source framework for proximity-based peer-to-peer applications

License

Notifications You must be signed in to change notification settings

webjinx/zyre.js

Repository files navigation

zyre.js

Build Status Build status codecov Greenkeeper badge

Node.js port of Zyre - an open-source framework for proximity-based peer-to-peer applications

Description

Zyre.js provides peer discovery and reliable (group) messaging over local area networks. Some of the key features:

  • Zyre.js works without administration or configuration
  • Peers can join or leave the network at any time
  • Peers can communicate directly with each other; no central server or message broker needed
  • Peers can join groups
  • Zyre.js loses no message, even when the network is under heavy load
  • Zyre.js is designed to be used in WiFi networks (but can also be used in Ethernet networks)
  • Peer discovery usually takes less than a second

Zyre.js implements the ZRE protocol.

Installation

npm install zyre.js

Documentation

A full jsdoc documentation can be found here.

Usage

const zyre = require('zyre.js');

Creates a new zyre.js instance (arguments are optional)

const zyre1 = zyre.new({
  name: 'foo',      // Name of the zyre node
  iface: 'eth0',    // Network interface
  headers: {        // Headers will be sent on every new connection
    foo: 'bar',
  },
  evasive: 5000,    // Timeout after which the local node will try to ping a not responding peer
  expired: 30000,   // Timeout after which a not responding peer gets disconnected
  bport: 5670,      // Discovery beacon broadcast port
  binterval: 1000,  // Discovery beacon broadcast interval
});

Starts up the zyre.js instance. Must be called before any other function

// Async function, so you can register...
zyre1.start(() => {
  // ...a callback or
}).then(() => {
  // ...a Promise
});

Stops the zyre.js instance. Deletes all peers data

// Async function, so you can register...
zyre1.stop(() => {
  // ...a callback or
}).then(() => {
  // ...a Promise
});

Sends a private message to the peer with the given identity

zyre1.whisper(identity, message);

Sends a message to the group with the given name

zyre1.shout(group, message);

Joins the group with the given name

zyre1.join(group);

Leaves the group with the given name

zyre1.leave(group);

Returns the identity of the local node

zyre1.getIdentity();

Returns information of the connected peer with the given identity

zyre1.getPeer(identity);

Returns information of all connected peers

zyre1.getPeers();

Returns information of the group with the given name

zyre1.getGroup(name);

Returns information of all known groups

zyre1.getGroups();

Connect is fired when a new peer joins the network

zyre1.on('connect', (id, name, headers) => {
  // ...
});

Disconnect is fired when a peer disconnects from the network

zyre1.on('disconnect', (id, name) => {
  // ...
});

Expired is fired when a peer timed out (uses expired timeout value)

zyre1.on('expired', (id, name) => {
  // ...
});

Whisper is fired when a peer sends a private message

zyre1.on('whisper', (id, name, message) => {
  // ...
});

Shout is fired when a peer sends a group message

zyre1.on('shout', (id, name, message, group) => {
  // ...
});

Join is fired when a peer joins a group

zyre1.on('join', (id, name, group) => {
  // ...
});

Leave is fired when a peer leaves a group

zyre1.on('leave', (id, name, group) => {
  // ...
});

Examples

There is a sample chat package that can be found here.

Inline example of two nodes talking to each other:

const zyre = require('zyre.js');

const zyre1 = zyre.new({ name: 'Chris' });
const zyre2 = zyre.new({ name: 'John' });

zyre1.on('whisper', (id, name, message) => {
  console.log(`<${name}> ${message}`);
});

zyre2.on('shout', (id, name, message, group) => {
  console.log(`#${group} <${name}> ${message}`);
  zyre2.whisper(id, `Hello ${name}!`);
});

zyre1.start(() => {
  zyre1.join('CHAT');
});

zyre2.start(() => {
  zyre2.join('CHAT');
});

setInterval(() => {
  zyre1.shout('CHAT', 'Hello World!');
}, 1000);

Prints:

#CHAT <Chris> Hello World!
<John> Hello Chris!

About

Node.js port of Zyre - an open-source framework for proximity-based peer-to-peer applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%