From 70060e8aa7b447bdf31f3fde33e7c14dd5d7028d Mon Sep 17 00:00:00 2001 From: Doug A Date: Mon, 19 Jun 2023 06:15:28 -0300 Subject: [PATCH] docs: Update readme docs to reflect v0.45.x api (#1821) Fixes https://github.com/libp2p/js-libp2p/issues/1810 --- examples/pubsub/README.md | 10 +++++----- examples/pubsub/message-filtering/README.md | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/pubsub/README.md b/examples/pubsub/README.md index fd2b861b97..ee8b5779ad 100644 --- a/examples/pubsub/README.md +++ b/examples/pubsub/README.md @@ -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) diff --git a/examples/pubsub/message-filtering/README.md b/examples/pubsub/message-filtering/README.md index ee9a301530..b6f11d55cb 100644 --- a/examples/pubsub/message-filtering/README.md +++ b/examples/pubsub/message-filtering/README.md @@ -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. @@ -97,9 +97,9 @@ 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. @@ -107,7 +107,7 @@ In this example, node one has an outdated version of the system, or is a malicio ```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)) } ```