Skip to content

Commit

Permalink
docs: pubsub example topic validator fix (#2094)
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki authored Sep 28, 2023
1 parent 91842c9 commit ae36e86
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions examples/pubsub/message-filtering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ First, let's update our libp2p configuration with a pubsub implementation.

```JavaScript
import { createLibp2p } from 'libp2p'
import { GossipSub } from '@chainsafe/libp2p-gossipsub'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { tcp } from '@libp2p/tcp'
import { mplex } from '@libp2p/mplex'
import { yamux } from '@chainsafe/libp2p-yamux'
Expand All @@ -23,7 +23,9 @@ const createNode = async () => {
streamMuxers: [yamux(), mplex()],
connectionEncryption: [noise()],
// we add the Pubsub module we want
pubsub: gossipsub({ allowPublishToZeroPeers: true })
services: {
pubsub: gossipsub({ allowPublishToZeroPeers: true })
}
})

return node
Expand Down Expand Up @@ -88,13 +90,17 @@ await node3.services.pubsub.subscribe(topic)
Finally, let's define the additional filter in the fruit topic.

```JavaScript
import { TopicValidatorResult } from '@libp2p/interface/pubsub'

const validateFruit = (msgTopic, msg) => {
const fruit = uint8ArrayToString(msg.data)
const validFruit = ['banana', 'apple', 'orange']

// car is not a fruit !
if (!validFruit.includes(fruit)) {
throw new Error('no valid fruit received')
}
return TopicValidatorResult.Accept
}

node1.services.pubsub.topicValidators.set(topic, validateFruit)
Expand Down

0 comments on commit ae36e86

Please sign in to comment.