From b24d1ff75845dc5308df2f00ee5110d0aeefbe6c Mon Sep 17 00:00:00 2001 From: Saul Date: Sat, 7 Jan 2023 04:03:16 +1300 Subject: [PATCH] chore: update readme code example to new API. (#382) * Update readme code example to new API. * Change readme code example to show how to use with libp2p. --- README.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 140b0dd7..bf6650d9 100644 --- a/README.md +++ b/README.md @@ -35,18 +35,21 @@ Gossipsub is an implementation of pubsub based on meshsub and floodsub. You can ## Usage ```javascript -const Gossipsub = require('@chainsafe/libp2p-gossipsub') +import { gossipsub } from '@chainsafe/libp2p-gossipsub' -const gsub = new Gossipsub(libp2p, options) -await gsub.start() +const libp2p = await createLibp2p({ + // ... + pubsub: gossipsub(options) +}); -gsub.on('fruit', (data) => { - console.log(data) +libp2p.pubsub.addEventListener('message', (message) => { + console.log(`${message.detail.topic}:`, new TextDecoder().decode(message.detail.data)) }) -gsub.subscribe('fruit') -gsub.publish('fruit', new TextEncoder().encode('banana')) +libp2p.pubsub.subscribe('fruit') + +libp2p.pubsub.publish('fruit', new TextEncoder().encode('banana')) ``` ## API @@ -55,7 +58,7 @@ gsub.publish('fruit', new TextEncoder().encode('banana')) ```js const options = {…} -const gossipsub = new Gossipsub(libp2p, options) +const gossipsub = gossipsub(options)(libp2p) ``` Options is an optional object with the following key-value pairs: