node-raft-over-mqtt
v1.2.1
Published
Implementing the Raft consensus algorithm over MQTT for building resilient and distributed systems across multiple nodes.
Downloads
14
Maintainers
Readme
Raft Node Presence Manager
A Node.js implementation for managing node presence in a distributed system using the Raft consensus algorithm and MQTT protocol.
Features
- Manages node presence and active nodes in a distributed network using the MQTT protocol.
- Implements the Raft consensus protocol for leader election and state management.
- Provides easy-to-use methods to handle MQTT connections, subscriptions, and messaging.
Installation
Install the package via npm:
npm install node-raft-over-mqtt
Libraries Used
This project makes use of the following libraries:
- node-mqtt-client: MQTT client for managing secure MQTT connections with certificate-based authentication.
Usage
Here's an example of how to use the NodePresenceManager
and RaftNode
classes:
Initialization
To set up the Raft node, create an instance and set the required properties.
const { MQTTClient, RaftNode } = require('node-raft-over-mqtt');
// Configure the MQTT client
const mqttClient = new MQTTClient();
mqttClient.host = 'broker.fabris.io';
mqttClient.port = 8883;
mqttClient.protocol = MQTTClient.Protocol.MQTTS;
mqttClient.certificateManager.loadCertificates(
'authority.pem',
'certificate.pem',
'key.pem'
);
mqttClient.connect();
// Unique identifier for the node
const uuid = 'node-1';
// Initialize the Raft node
const raftNode = RaftNode.initialize(mqttClient, uuid);
// Set the logging function
raftNode.logFunction = console.log;
// Start the node
raftNode.start();
MQTT Client Configuration
For detailed configuration options and parameters for the MQTTClient
, please refer to the official node-mqtt-client documentation.
Configuration Parameters for RaftNode
| Parameter | Type | Description | Default |
|-----------------------|------------|---------------------------------------------------------------------|----------------|
| electionTimeout
| number
| The timeout duration in milliseconds for the election process. | 8000
|
| heartbeatInterval
| number
| The interval in milliseconds between heartbeats sent by the leader. | 2000
|
| metric
| number
| The value used as the node's metric for leader elections. Lower values are considered better. |
RaftNode Public Variables
presenceManager
(Read-only)
Returns the instance of the NodePresenceManager
associated with the node.
- Returns:
NodePresenceManager
- The presence manager instance.
logFunction
Gets or sets the log function.
- Get: Returns the current log function.
- Returns:
Function
- The log function.
- Returns:
- Set: Sets a new log function for logging messages.
- Parameters:
func
(Function
): The new log function.
- Parameters:
metric
Gets or sets the metric value used in leader elections.
Get: Returns the current metric value.
- Returns:
number
- The metric value.
- Returns:
Set: Sets a new metric value.
- Parameters:
value
(number
): The new metric value.
- Parameters:
leaderMetric
(Read-only)
Gets the leader's metric value.
- Returns:
number
- The current leader metric.
RaftNode Public Methods
initialize(mqttClient, uuid)
Initializes a new RaftNode instance if it does not exist.
mqttClient
(MQTTClient
): The MQTT client instance.uuid
(string
): The unique identifier for the node.- Returns:
RaftNode
- The instance of the RaftNode.
- Returns:
log(message)
Logs a message using the provided log function.
message
(string
): The message to log.
start()
Starts the Raft node and initializes its state.
isLeader()
Checks if the current node is the leader.
- Returns:
boolean
-true
if the node is a leader, otherwisefalse
.
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- Paolo Fabris - Initial work - ubyte.it
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License. See the LICENSE file for details.