Skip to content

Latest commit

 

History

History
170 lines (133 loc) · 4.93 KB

rpc.md

File metadata and controls

170 lines (133 loc) · 4.93 KB

RPC

The RPC allows the node to connect with other nodes, communicate with them as well as receiving messages from other nodes.

Contents

Constructor

new RPC(options)

  • options:
    • port: Integer (Default: 8080) UDP port of the node.


Creates a new RPC.

const plexus = require("plexus");

//  Creating a new RPC
const rpc = new plexus.RPC();

Methods

rpc.create_id()


Creates a pracket ID.

//  Create and ID for the packet to send
const id = rpc.create_id();

rpc.send_request({host, port}, id)

  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.
  • id: String Request ID.


Sends a request packet.

const host = "127.0.0.1";
const port = 8080;

const id = rpc.create_id();

rpc.send_request({host, port}, id);

rpc.send_acknowledge({host, port}, id)

  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.
  • id: String Request ID.


Sends an acknowledgement packet.

const host = "127.0.0.1";
const port = 8080;

//  Incoming request with ID = 63b8fa2e7d0af923fb0505340bcad6a5
const id = "63b8fa2e7d0af923fb0505340bcad6a5";

rpc.send_acknowledge({host, port}, id);

rpc.message_type(message)

  • message: Message The messsage to get the type from.


Returns the type of a message (REQUEST, RESPONSE or UNKNOWN).

//  Create a request
const request = new plexus.Message({ method: "remote method", params: {} });

//  Get the type of the message
const type = rpc.message_type(request);
console.log(type);  //  REQUEST (0)

rpc.handshake({host, port}, attempts, timeout)

  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.
  • attempts: Integer (Default: 60) The amount of requests to send.
  • timeout: Integer (Default: 1000) The interval between requests.


Initiates a handshake negotiation with a remote node.

const handshake = rpc.handshake({host, port}, attempts, timeout);

handshake.on("connected", () => {
    console.log("connected");
});

handshake.on("timeout", () => {
    console.log("timed out");
});

rpc.send_message(message, {host, port}, attempts, timeout)

  • message: Message The messsage to send to the remote node.
  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.
  • attempts: Integer (Default: 60) The amount of requests to send.
  • timeout: Integer (Default: 1000) The interval between requests.


Sends a message to a remote node.

const request = new plexus.Message({ method: "remote method", params: {} });

const handshake = rpc.send_message(request, {host, port});

handshake.on("response", (message, {host, port}) => {
    console.log("request got a response");
});

Events

Event 'ready'

Emitted when the RPC is ready.

Event 'message'

  • message: Message The message sent to the node.
  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.

Event 'PING'

  • message: Message The message sent to the node.
  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.

Event 'FIND'

  • message: Message The message sent to the node.
  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.

Event 'STORE'

  • message: Message The message sent to the node.
  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.

Event 'BROADCAST'

  • message: Message The message sent to the node.
  • host: String IP address of the remote node.
  • port: Integer UDP port of the remote node.