Skip to content

Commit

Permalink
docs: Update readme docs to reflect v0.45.x api (#1821)
Browse files Browse the repository at this point in the history
Fixes #1810
  • Loading branch information
DougAnderson444 authored and achingbrain committed Jun 19, 2023
1 parent 25504ae commit 70060e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions examples/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ await node1.peerStore.patch(node2.peerId, {
})
await node1.dial(node2.peerId)

node1.pubsub.addEventListener("message", (evt) => {
node1.services.pubsub.addEventListener("message", (evt) => {
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`)
})
await node1.pubsub.subscribe(topic)
await node1.services.pubsub.subscribe(topic)

// Will not receive own published messages by default
node2.pubsub.addEventListener("message", (evt) => {
node2.services.pubsub.addEventListener("message", (evt) => {
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`)
})
await node2.pubsub.subscribe(topic)
await node2.services.pubsub.subscribe(topic)

// node2 publishes "news" every second
setInterval(() => {
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!')).catch(err => {
node2.services.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!')).catch(err => {
console.error(err)
})
}, 1000)
Expand Down
20 changes: 10 additions & 10 deletions examples/pubsub/message-filtering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,32 @@ import { toString as uint8ArrayToString } from "uint8arrays/to-string";

const topic = 'fruit'

node1.pubsub.addEventListener('message', (msg) => {
node1.services.pubsub.addEventListener('message', (msg) => {
if (msg.detail.topic !== topic) {
return
}

console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
})
await node1.pubsub.subscribe(topic)
await node1.services.pubsub.subscribe(topic)

node2.pubsub.addEventListener('message', (msg) => {
node2.services.pubsub.addEventListener('message', (msg) => {
if (msg.detail.topic !== topic) {
return
}

console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
})
await node2.pubsub.subscribe(topic)
await node2.services.pubsub.subscribe(topic)

node3.pubsub.addEventListener('message', (msg) => {
node3.services.pubsub.addEventListener('message', (msg) => {
if (msg.detail.topic !== topic) {
return
}

console.log(`node3 received: ${uint8ArrayToString(msg.data)}`)
})
await node3.pubsub.subscribe(topic)
await node3.services.pubsub.subscribe(topic)
```
Finally, let's define the additional filter in the fruit topic.

Expand All @@ -97,17 +97,17 @@ const validateFruit = (msgTopic, msg) => {
}
}

node1.pubsub.topicValidators.set(topic, validateFruit)
node2.pubsub.topicValidators.set(topic, validateFruit)
node3.pubsub.topicValidators.set(topic, validateFruit)
node1.services.pubsub.topicValidators.set(topic, validateFruit)
node2.services.pubsub.topicValidators.set(topic, validateFruit)
node3.services.pubsub.topicValidators.set(topic, validateFruit)
```

In this example, node one has an outdated version of the system, or is a malicious node. When it tries to publish fruit, the messages are re-shared and all the nodes share the message. However, when it tries to publish a vehicle the message is not re-shared.

```JavaScript
for (const fruit of ['banana', 'apple', 'car', 'orange']) {
console.log('############## fruit ' + fruit + ' ##############')
await node1.pubsub.publish(topic, uint8ArrayFromString(fruit))
await node1.services.pubsub.publish(topic, uint8ArrayFromString(fruit))
}
```

Expand Down

0 comments on commit 70060e8

Please sign in to comment.