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

zyre.js

v1.3.0

Published

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

Downloads

8

Readme

zyre.js

Test

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.

Zyre.js 2.x is currently available as beta, which uses the rewritten zeromq.js 6.x

Installation

npm install zyre.js

Beta:

npm install zyre.js@beta

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 zyre = new Zyre({
  name: 'foo',      // Name of the zyre node
  iface: 'eth0',    // Network interface or IPv4 address
  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
  port: 49152,      // Port for incoming messages, will be incremented if already in use
  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...
zyre.start(() => {
  // ...a callback or
}).then(() => {
  // ...a Promise
});

Stops the zyre.js instance. Deletes all peers data.

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

Sends a private message to the peer with the given identity. The message can be a string or a raw buffer.

zyre.whisper(identity, message);

Sends a message to the group with the given name. The message can be a string or a raw buffer.

zyre.shout(group, message);

Joins the group with the given name.

zyre.join(group);

Leaves the group with the given name.

zyre.leave(group);

Returns the identity of the local node. The identity is a 16 byte UUID as hex string.

zyre.getIdentity();

Returns information of the connected peer with the given identity.

zyre.getPeer(identity);

Returns information of all connected peers.

zyre.getPeers();

Returns information of the group with the given name.

zyre.getGroup(name);

Returns information of all known groups.

zyre.getGroups();

Sets the encoding of received messages. Defaults to utf8. Available are: ascii, utf8, utf16le/ucs2, base64, binary, hex, raw/null

zyre.setEncoding('utf8');   // Default encoding
zyre.setEncoding(null);     // Raw buffers

Connect is fired when a new peer joins the network.

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

Disconnect is fired when a peer disconnects from the network.

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

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

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

Whisper is fired when a peer sends a private message.

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

Shout is fired when a peer sends a group message.

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

Join is fired when a peer joins a group.

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

Leave is fired when a peer leaves a group.

zyre.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 chris = new Zyre({ name: 'Chris' });
const john = new Zyre({ name: 'John' });

chris.on('connect', () => {
  chris.shout('CHAT', 'Hello World');
});

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

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

john.on('shout', (id, name, message, group) => {
  console.log(`(${group}) ${name}: ${message}`);
  john.whisper(id, `Hello ${name}`);
});

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

Prints:

(CHAT) Chris: Hello World
John: Hello Chris