libp2p-floodsub
v0.29.1
Published
libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).
Downloads
4,821
Readme
js-libp2p-floodsub
libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).
Lead Maintainer
Table of Contents
Install
> npm install libp2p-floodsub
Usage
const FloodSub = require('libp2p-floodsub')
// registrar is provided by libp2p
const fsub = new FloodSub(peerId, registrar, options)
await fsub.start()
fsub.on('fruit', (data) => {
console.log(data)
})
fsub.subscribe('fruit')
fsub.publish('fruit', new TextEncoder().encode('banana'))
API
Create a floodsub implementation
const options = {…}
const floodsub = new Floodsub(peerId, registrar, options)
Options is an optional object with the following key-value pairs:
emitSelf
: boolean identifying whether the node should emit to self on publish, in the event of the topic being subscribed (defaults to false).
For the remaining API, see https://github.com/libp2p/js-libp2p-pubsub
Events
Floodsub emits two kinds of events:
<topic>
when a message is received for a particular topic
fsub.on('fruit', (data) => { ... })
data
: a Uint8Array containing the data that was published to the topic
floodsub:subscription-change
when the local peer receives an update to the subscriptions of a remote peer.
fsub.on('floodsub:subscription-change', (peerId, topics, changes) => { ... })
peerId
: a PeerId objecttopics
: the topics that the peer is now subscribed tochanges
: an array of{ topicID: <topic>, subscribe: <boolean> }
eg[ { topicID: 'fruit', subscribe: true }, { topicID: 'vegetables': false } ]
Contribute
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.
License
Copyright (c) Protocol Labs, Inc. under the MIT License. See LICENSE file for details.