The RPC allows the node to connect with other nodes, communicate with them as well as receiving messages from other nodes.
- constructor
- methods
- rpc.create_id()
- rpc.error_handler(error)
- rpc.message_handler(bytes, rinfo)
- rpc.send_request({host, port}, id)
- rpc.send_acknowledge({host, port}, id)
- rpc.on_request({host, port}, bytes)
- rpc.on_acknowledge({host, port}, bytes)
- rpc.on_message({host, port}, bytes)
- rpc.message_type(message)
- rpc.handshake({host, port}, attempts, timeout)
- rpc.send_message(message, {host, port}, attempts, timeout)
- events
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();
Creates a pracket ID.
// Create and ID for the packet to send
const id = rpc.create_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);
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);
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)
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");
});
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");
});
Emitted when the RPC is ready.
message
: Message The message sent to the node.host
: String IP address of the remote node.port
: Integer UDP port of the remote node.
message
: Message The message sent to the node.host
: String IP address of the remote node.port
: Integer UDP port of the remote node.
message
: Message The message sent to the node.host
: String IP address of the remote node.port
: Integer UDP port of the remote node.
message
: Message The message sent to the node.host
: String IP address of the remote node.port
: Integer UDP port of the remote node.
message
: Message The message sent to the node.host
: String IP address of the remote node.port
: Integer UDP port of the remote node.