Last active
April 5, 2021 17:20
libp2p pubsub setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Libp2p = require('libp2p') | |
const Gossipsub = require('libp2p-gossipsub') | |
const node = await Libp2p.create({ | |
modules: { | |
// … other required modules | |
pubsub: Gossipsub | |
} | |
}) | |
await node.start() | |
const topic = 'kickoff-your-application-with-js-libp2p' | |
node.pubsub.subscribe(topic) | |
node.pubsub.on(topic, (message) => { | |
// Handle message | |
}) | |
// Publish a new message each second | |
setInterval(() => { | |
libp2p.pubsub.publish(topic, new Uint8Array([21, 31])) | |
}, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment