Skip to content

Latest commit

 

History

History
140 lines (109 loc) · 4.01 KB

node.md

File metadata and controls

140 lines (109 loc) · 4.01 KB

Node

Nodes are the heart of the Plexus Network, they allow your application to connect and interact with the network.

Contents

Constructor

new Node(options)

  • options:
    • host: String (Default: "127.0.0.1") Local IP address of the node.
    • port: Integer (Default: 8080) UDP port of the node.
    • id: String The local node ID.
    • capacity: Integer (Default: 160) The maximum amount of buckets to be stored in the router.
    • peers: Integer (Default: 20) The maximum amount of contacts to be stored per bucket.
    • refresh: Integer (Default: 3600000) Buckets refresh interval.
    • expire: Integer (Default: 86400000) Item expiration time.
    • republish: Integer (Default: 86400000) Item republishing interval (only for the item publisher).
    • replicate: Integer (Default: 3600000) Data replication interval.


Creates a new Node instance.

const plexus = require("plexus");

//  Node creation
const node = new plexus.Node({host: "127.0.0.1", port: 8080});

Methods

node.connect({ host, port })

  • host: String Remote IP of the node to connect to.
  • port: Integer Remote UDP port of the node to connect to.


Connects the node to the network.

//  Join the network
node.connect({host: "remote_ip", port: remote_port});

node.broadcast({ data })

  • data: Object The data to broadcast through the network.


Broadcasts to the whole Network.

//  Sendind to the Network
node.broadcast({data: data});

//  Handling incoming Broadcasts
node.on("broadcast", (data) => {
    console.log(data);
});

node.message({ message, id })

  • message: Any The data to send to the remote node.
  • id: String The ID of the remote node.


Sends a message to a remote node.

//  Sendind to the remote node
node.message({message: "message", id: "id"});

//  Handling incoming Messages
node.on("message", (message, sender) => {
    console.log(message, sender);
});

node.store({ key, value, republish })

  • key: String (Optional, Default: hash-of-value) The key used to find and store the value on the network.
  • value: Object The value to store on the network.
  • republish: Boolean (Optional, Default: false) Whether or not to republish previously published items after they expire.


Stores data on the Network.

//  Storing data
const item = node.store({key: key, value: value, republish: true});

node.find({ key, type })

  • key: String The key used to find and store the value.
  • type: String The type of item to find ("item", "node").


Retrieving data on the Network.

//  Retrieving data
const lookup = node.find({key: key, type: "node"});

//  The item exists on the Network
lookup.on("found", (result) => {
    console.log(result);
});

//  The item doesn't exist anywhere on the Network
lookup.on("timeout", () => {
    console.log("FIND request timed out");
});

Events

Event 'ready'

Emitted when the local node is ready.

Event 'broadcast'

  • data: Object The data broadcasted through the network.

Emitted when another node issues a broadcast request to this node.

Event 'message'

  • message: Any The data received from a remote node.
  • sender: Object The remote node's informations.

Emitted when another node sends a message to this node.

Event 'connected'

  • contact: Contact The key used to find and store the value.

Emitted when the local node successfully connects to another one.