Registry / messaging / node-raft-redis

node-raft-redis

JSON →
library0.1.1jsnpmunverified

A consensus library for Node.js microservices implementing a simplified Raft algorithm using Redis as the communication medium. Version 0.1.1 is the latest stable release; the project is in early development with no frequent releases. It provides automatic leader election among instances of the same service kind, supporting leader/follower roles, messaging, and instance metadata. Unlike full Raft implementations, this is a simplified approach focused on microservice coordination, requiring only Redis and offering easy integration without complex setup.

npm install node-raft-redis
INSTALL
IMPORT
SIG · NODE-RAFT-REDIS
N
node-raft-redis
messagingjavascriptv0.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Candidate
import { Candidate } from 'node-raft-redis'
const Candidate = require('node-raft-redis')
The package exports Candidate as a named export. CommonJS require will not work as expected; use ESM imports or check if the package supports CJS interop.
CandidateOptions
import type { CandidateOptions } from 'node-raft-redis'
import { CandidateOptions } from 'node-raft-redis'
CandidateOptions is a TypeScript type/interface and should be imported with 'import type' to avoid runtime errors.
InstanceInfo
import type { InstanceInfo } from 'node-raft-redis'
import { InstanceInfo } from 'node-raft-redis'
InstanceInfo is a TypeScript type/interface.

Create a Candidate instance, listen for events (elected, defeated, message), and start the consensus process.

import { Candidate } from 'node-raft-redis'; const candidate = new Candidate({ redis: { host: 'localhost', port: 6379 }, kind: 'my-service', }); candidate.on('elected', () => { console.log('I am the leader'); candidate.messageFollowers('Hello from leader'); }); candidate.on('defeated', (leader) => { console.log(`New leader is ${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); }); await candidate.start();
Debug
Known issues
gotchaRedis connection options must be provided as an object with 'host' and 'port', or a 'url' string. Mixing both may cause unexpected behavior.
fix
Use either redis: { host: '...', port: 6379 } or redis: { url: 'redis://...' }, not both.
affects: <=0.1.1
gotchaThe 'kind' option must be a non-empty string. Using an empty string or no 'kind' will cause errors.
fix
Always provide a valid string for 'kind'.
affects: <=0.1.1
gotchaThe 'reelect()' method can only be called by the leader. Calling it from a follower throws an error.
fix
Check if candidate is leader by listening to 'elected' and only call reelect from that context.
affects: <=0.1.1
deprecatedThe 'meta' option in constructor is not stable; it may be changed in future versions.
fix
Use setMeta() method after candidate.start() to set metadata.
affects: <=0.1.1
gotchaThe package only supports Node.js with TypeScript. Running in a browser will fail due to Redis requirement.
fix
Ensure the code runs in a Node.js environment with a Redis server accessible.
affects: <=0.1.1
Errors
Common errors & fixes
Error: Redis connection failed: connect ECONNREFUSED 127.0.0.1:6379
Redis server is not running or not accessible at the specified host/port.
fix
Start Redis server or set correct connection options via redis option in constructor.
TypeError: Cannot destructure property 'message' of undefined
The 'message' event listener expected a 'message' property but received undefined because the event payload might be malformed or the event name is misspelled.
fix
Ensure the event listener uses correct signature: (payload) => { ... } where payload has 'message' and 'from' properties. If using custom events, check for typos.
Error: kind must be a non-empty string
Passed an empty string or non-string value for 'kind' option.
fix
Provide a valid string for 'kind' in the Candidate constructor.
Error: Cannot start candidate because it is already running
Called candidate.start() multiple times without calling stop() in between.
fix
Check candidate state or call candidate.stop() before starting again.
Upgrade
Version history
0.1.1latest on npm
Audit
Dependencies
ioredisrequiredRedis client for communication and state storage
redisoptionalAlternative Redis client - the package may support either ioredis or node-redis
Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
node-raft-redis — npm install node-raft-redis · libregistry