Skip to content

Commit

Permalink
chore: update readme code example to new API. (#382)
Browse files Browse the repository at this point in the history
* Update readme code example to new API.

* Change readme code example to show how to use with libp2p.
  • Loading branch information
saul-jb authored Jan 6, 2023
1 parent 9bb8ecd commit b24d1ff
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit b24d1ff

Please sign in to comment.