Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Update readme docs to reflect v0.45.x api #1821

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,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