deluge
Send broadcast messages on top of p2p networks
Install
$ npm install @geut/deluge
Usage
const { Deluge } = require('@geut/deluge')
const deluge = new Deluge()
// add your peers into deluge
deluge.addPeer(peer.id, {
send (packet) {
// send data
peer.write(packet.buffer)
},
subscribe (next) {
// subscribe for incoming data
peer.on('data', next)
// returns unsubscribe function
return () => {
peer.off('data', next)
}
}
})
deluge.addPeer(...)
// listen for incoming packets
deluge.on('packet', (packet) => {
console.log(packet.data.toString()) // ping
})
// send a broadcast ping message in the channel = 0
deluge.send(0, Buffer.from('ping'))
API
deluge = new Deluge(opts?)
-
opts?: Object = {}
-
onPeer?: OnPeerCallback
Callback to pre-process a new peer. -
onPacket?: OnPacketCallback
Async callback to filter incoming packets. -
onSend?: OnSendCallback
Async callback to filter peers before to send a packet.
-
deluge.ready() => Promise<any>
Wait for the deluge to be opened.
deluge.open(id?) => Promise<any>
Open deluge with a Buffer ID.
id?: Buffer = crypto.randomBytes(32)
deluge.onPeer(callback) => void
callback: OnPeerCallback
deluge.onPacket(callback) => void
callback: OnPacketCallback
deluge.onSend(callback) => void
callback: OnSendCallback
deluge.getPeer(key) => Peer | undefined
Get a peer by key.
key: Buffer | string
deluge.addPeer(key, handler) => Promise<Peer>
Add a new peer into the deluge network.
key: Buffer | string
handler: Peer.Handler
deluge.deletePeer(key) => Promise<any>
key: Buffer | string
deluge.send(channel, data) => Promise<Packet | undefined>
Broadcast a flooding message into the deluge network.
channel: number
data: Buffer
deluge.createDuplexStream(opts?) => Duplex
Create a new Duplex Streamx.
opts?: any = {}
deluge.id: Buffer | null
deluge.peers: Peer[] (R)
Handler: {}
send: (packet: any) > undefined
subscribe: (data: Buffer) > UnsubscribeFunction
packet = new Packet(opts)
-
opts: Object
origin: Buffer
data: Uint8Array
channel?: number = 0
seqno?: TimestampSeq
from?: Uint8Array
distance?: number = 0
buffer?: Buffer
timestampSeq = new TimestampSeq(timestamp, offset)
timestamp: any
offset: any
timestampSeq.compare(value) => number
Compare two TimestampSeq (self and value).
Returns: - 0 if they are equals. - 1 if self is major than value - -1 if self is minor than value
value: TimestampSeq
Issues
Contributing
License
MIT © A GEUT project