secure-gossip
Secure, transport agnostic, message gossip protocol.
Any peer in the network can publish messages, which will eventually propogate out to the entire network via rounds of gossip between each node's peers.
Security
Each published message is signed with the peer's private key, using ssb-keys. This means messages cannot be forged -- you can be sure that the messages you receive are from the peer that it claims to be, even if it came in from an intermediary.
Example
Let's create a line of 3 peers and watch a published message propogate through them:
var gossip = var peer1 = var peer2 = var peer3 = var p1 = peer1var p2_1 = peer2var p2_2 = peer2var p3 = peer3 // 3 peers in a line, with peer-2 in the middlep1p2_2 // have p1 publish, and watch it propogate to p2 and then p3peer1 peer1 peer2 peer3
outputs
p2 message { data: "hello warld" } from UHXoKlI2blLU0GaAC3G2sUSGBUOWY7yYpN7DhWQI2/Y=.ed25519
p3 message { data: "hello warld" } from UHXoKlI2blLU0GaAC3G2sUSGBUOWY7yYpN7DhWQI2/Y=.ed25519
API
var gossip =
var peer = gossip(opts={})
Returns a new gossiping peer. Accepts various options via opts
:
keys
- uses keypairkeys
for message signing. You can generate these keys yourself (usingssb-keys
), or, if not given, a fresh keypair will be generated for you.interval
- how often to perform gossip with peers, in milliseconds. Defaults to 100ms. If set to-1
, no automatic gossip will occur and you'll need to runpeer.gossip()
manually.
peer.createPeerStream()
Returns a new duplex stream that can be used to exchange gossip with another peer.
Remember: create a new peer stream per pair of peers!
peer.publish(msg)
Publish a signed message to all of your peers.
peer.gossip()
Initiate a round of gossip manually, forwarding all stored messages to peers.
peer.stop()
Stops gossiping (clears the internal setInterval
timer).
peer.on('message', function (msg, info) {})
Event fired when a message is received from a peer.
msg
is the original payload from the peerinfo
is an object, currently only having the fieldpublic
, which is a string of the sender's public key
install
With npm installed, run
$ npm install secure-gossip
license
ISC