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

node-raft-redis

v0.1.1

Published

Consensus for node microservices, based on a simplified version of the [raft algorithm](https://raft.github.io/). Requires redis as a medium. Features automatic discovery of the services of same kind. Selects one instance of a microservice as leader. For

Downloads

2,013

Readme

Consensus for node microservices, based on a simplified version of the raft algorithm. Requires redis as a medium. Features automatic discovery of the services of same kind. Selects one instance of a microservice as leader. For example, the leader can push jobs to a redis queue while the followers process them.


Usage:

import { Candidate } from "./index";

const candidate = new Candidate({
  redis: {
    host: "localhost",
    port: 6379,
    // or url: 'redis://alice:[email protected]:6380'
  },
  kind: "my-service",
});

candidate.on("elected", () => {
  console.log("elected");
  // You can be sure only this instance is the leader (at the moment).

  candidate.messageFollowers("Hello from leader");

  // The leader can start a re-election with reelect()
  // It is possible for this instance to be elected again. In that case, "elected" will be emitted again.
  // candidate.reelect();
});

candidate.on("defeated", (leader) => {
  console.log(`defeated by the new leader ${leader}`)
  candidate.messageLeader("Hello from your new follower")
});

candidate.on("message", ({ message, from }) => {
  console.log(`Got message ${message} from ${from}`)
});

candidate.on("error", (err) => {
  console.error(err);
});

candidate.start();

Examples:


const candidate = new Candidate({
  redis: {
    host: "localhost",
    port: 6379,
  },
  kind: "my-service",
  meta: {
    // Set some metadata for this instance
    someKey: Math.random(),
  },
});

await candidate.start();
// Get all instances of this kind, including self
const instances = await candidate.getInstances()
/*
[
  {
    id: 'rqcpc6g0ncgi3mxtysyv1',
    meta: { someKey: 0.8191968688804481 },
    state: 'follower'
  },
  {
    id: 'ga4empryhsch6mi5zlemgn',
    meta: { someKey: 0.5482506611623577 },
    state: 'follower'
  },
  {
    id: 'qtualcg9ncgq62eruoqe18',
    meta: { someKey: 0.6551732192131019 },  
    state: 'leader'
  },
  {
    id: 'aow8uegf3241mt95pqhte3',
    meta: { someKey: 0.7014849186816583 },  
    state: 'follower'
  }
]
*/

// Send a message to an instance
candidate.messageTo('rqcpc6g0ncgi3mxtysyv1', "Hello")

// Send a message to all instances
candidate.messageAll("Hello")

// Send a message to the follower instances
candidate.messageFollowers("Hello from leader")

// Send a message to the leader
candidate.messageLeader("Hello from follower")

// Get the instance's metadata
const meta = candidate.getMeta()

// Change the instance's metadata
await candidate.setMeta({
  someKey: "someValue"
})
candidate.on("message:from:leader", ({ message, from }) => {
// Will be logged only in the follower instances.
  console.log(`Got message ${message} from the leader instance ${from}`);
});

candidate.on("message:from:follower", ({ message, from }) => {
  // Will be logged only in the leader instance.
  console.log(`Got message ${message} from the follower instance ${from}`);
});
candidate.on("message", async ({ message, from }) => {
  const currentLeader = await candidate.getLeader();
  const isFromLeader = from === currentLeader.id;
  
  if (isFromLeader) {
    console.log(`Got message ${message} from the leader instance ${from}`);
    candidate.messageTo(from, "Echo from follower");
    // candidate.messageLeader("Echo from follower"); <- same thing in this specific case
  } else {
    console.log(`Got message ${message} from the follower instance ${from}`);
  }
});

Methods (all async):

  • start: connects to redis, starts the candidate's internal processes
  • stop: disconnects from redis, stops the candidate's internal processes
  • reelect: if the candidate is the leader, starts a re-election
  • messageFollowers: send message to the followers
  • messageLeader: send message to the leader
  • messageTo: send a message to the specific instance
  • messageAll: send a message to all of the instances
  • getInstances: get the instances of this kind along with their metadata
  • setMeta: set the current instance's metadata

Properties:

  • id: the current instance's unique generated ID
  • state: "follower" | "candidate" | "leader"
  • currentTerm: number
  • meta: the current instance's metadata

Events:

  • elected: emitted on the following state transitions:
    1. candidate -> leader
  • defeated: emitted on the following state transitions:
    1. leader -> follower
    2. candidate -> follower
  • message: message from the leader or the followers
  • message:from:follower: message from the followers
  • message:from:leader: message from the leader
  • statechange: on state transition
  • error: redis connection error