Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Last active April 5, 2021 17:20
Show Gist options
  • Save vasco-santos/a9ce459f0c64c45e796dcaa2ae030252 to your computer and use it in GitHub Desktop.
Save vasco-santos/a9ce459f0c64c45e796dcaa2ae030252 to your computer and use it in GitHub Desktop.
libp2p pubsub setup
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