Skip to content

Commit ae36e86

Browse files
authored
docs: pubsub example topic validator fix (#2094)
1 parent 91842c9 commit ae36e86

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

examples/pubsub/message-filtering/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ First, let's update our libp2p configuration with a pubsub implementation.
88

99
```JavaScript
1010
import { createLibp2p } from 'libp2p'
11-
import { GossipSub } from '@chainsafe/libp2p-gossipsub'
11+
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
1212
import { tcp } from '@libp2p/tcp'
1313
import { mplex } from '@libp2p/mplex'
1414
import { yamux } from '@chainsafe/libp2p-yamux'
@@ -23,7 +23,9 @@ const createNode = async () => {
2323
streamMuxers: [yamux(), mplex()],
2424
connectionEncryption: [noise()],
2525
// we add the Pubsub module we want
26-
pubsub: gossipsub({ allowPublishToZeroPeers: true })
26+
services: {
27+
pubsub: gossipsub({ allowPublishToZeroPeers: true })
28+
}
2729
})
2830

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

9092
```JavaScript
93+
import { TopicValidatorResult } from '@libp2p/interface/pubsub'
94+
9195
const validateFruit = (msgTopic, msg) => {
9296
const fruit = uint8ArrayToString(msg.data)
9397
const validFruit = ['banana', 'apple', 'orange']
9498

99+
// car is not a fruit !
95100
if (!validFruit.includes(fruit)) {
96101
throw new Error('no valid fruit received')
97102
}
103+
return TopicValidatorResult.Accept
98104
}
99105

100106
node1.services.pubsub.topicValidators.set(topic, validateFruit)

0 commit comments

Comments
 (0)