External API - HelloZeroNet/Plugin-PeerMessage GitHub Wiki

Actions

peerBroadcast

Broadcasts a message to all the network.

Arguments:

  1. message - the string/object/anything you want to broadcast.
  2. privatekey=None - how to sign the message (see below).
  3. peer_count=5 - the count of peers to connect to.
  4. immediate=False - if True and the browser is closed, the message will be saved in memory and flushed to browser when the site is opened.

peerSend

Send message to one IP.

  1. ip - IP to send the message to.
  2. message - data to send.
  3. privatekey=None - how to sign the message (see below).
  4. to=None - reply to. (see below for example)
  5. immediate=False - same as in peerBroadcast (read above).

It's quite difficult to get peer IP. It's recommended to use this method only if you know the real IP - from whatismyipaddress service or IPX (huh, that's defenitely NOT what you thought about).

Example

// Send message to IP
let IP = "1.2.3.4:5";
zeroFrame.cmd("peerSend", {ip: IP, message: "ping"}, function(result) {
    console.log("Received", result.message);
});

zeroFrame.onRequest = function(cmd, message) {
    if(cmd == "peerReceive") {
        // Validate message. Read below
        zeroFrame.cmd("peerValid", [message.params.hash]);

        let fromIP = message.params.ip; // That's where we got the message from
        let fromHash = message.params.hash; // That's the ID of the message
        zeroFrame.cmd("peerSend", {ip: fromIP, to: fromHash, message: "pong"}); // Reply
    }
};

peerInvalid and peerValid

Sets the message as invalid or valid. Valid messages are broadcast to other peers, those who send invalid messages are banned.

Arguments:

  1. hash - the hash of the message you received.

Always. No - ALWAYS run peerInvalid and peerValid. NEVER forget it, or messages won't be broadcast.