Package Exports
- libp2p-floodsub
- libp2p-floodsub/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (libp2p-floodsub) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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.