From 8341af05e80d04bff11a060a23b970027cb1dd75 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 4 Dec 2020 13:29:04 +0100 Subject: [PATCH] chore: update pubsub example by disabled emit self --- examples/pubsub/1.js | 1 + examples/pubsub/README.md | 19 ++++++++++++++----- examples/pubsub/message-filtering/1.js | 1 + examples/pubsub/message-filtering/README.md | 3 --- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/examples/pubsub/1.js b/examples/pubsub/1.js index 8c6fdfdb91..419838960b 100644 --- a/examples/pubsub/1.js +++ b/examples/pubsub/1.js @@ -43,6 +43,7 @@ const createNode = async () => { }) await node1.pubsub.subscribe(topic) + // Will not receive own published messages by default node2.pubsub.on(topic, (msg) => { console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) }) diff --git a/examples/pubsub/README.md b/examples/pubsub/README.md index 17a896f3a6..2016b2c813 100644 --- a/examples/pubsub/README.md +++ b/examples/pubsub/README.md @@ -44,7 +44,6 @@ const node2 = nodes[1] // Add node's 2 data to the PeerStore node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs) - await node1.dial(node2.peerId) node1.pubsub.on(topic, (msg) => { @@ -52,6 +51,7 @@ node1.pubsub.on(topic, (msg) => { }) await node1.pubsub.subscribe(topic) +// Will not receive own published messages by default node2.pubsub.on(topic, (msg) => { console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) }) @@ -68,25 +68,34 @@ The output of the program should look like: ``` > node 1.js connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 -node2 received: Bird bird bird, bird is the word! node1 received: Bird bird bird, bird is the word! -node2 received: Bird bird bird, bird is the word! node1 received: Bird bird bird, bird is the word! ``` -You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages. +You can change the pubsub `emitSelf` option if you want the publishing node to receive its own messages. ```JavaScript const defaults = { config: { pubsub: { enabled: true, - emitSelf: false + emitSelf: true } } } ``` +The output of the program should look like: + +``` +> node 1.js +connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 +node1 received: Bird bird bird, bird is the word! +node2 received: Bird bird bird, bird is the word! +node1 received: Bird bird bird, bird is the word! +node2 received: Bird bird bird, bird is the word! +``` + ## 2. Future work libp2p/IPFS PubSub is enabling a whole set of Distributed Real Time applications using CRDT (Conflict-Free Replicated Data Types). It is still going through heavy research (and hacking) and we invite you to join the conversation at [research-CRDT](https://github.com/ipfs/research-CRDT). Here is a list of some of the exciting examples: diff --git a/examples/pubsub/message-filtering/1.js b/examples/pubsub/message-filtering/1.js index 85d7bcf8c4..4d8a2c1803 100644 --- a/examples/pubsub/message-filtering/1.js +++ b/examples/pubsub/message-filtering/1.js @@ -44,6 +44,7 @@ const createNode = async () => { //subscribe node1.pubsub.on(topic, (msg) => { + // Will not receive own published messages by default console.log(`node1 received: ${uint8ArrayToString(msg.data)}`) }) await node1.pubsub.subscribe(topic) diff --git a/examples/pubsub/message-filtering/README.md b/examples/pubsub/message-filtering/README.md index a9c0dad26d..df99043051 100644 --- a/examples/pubsub/message-filtering/README.md +++ b/examples/pubsub/message-filtering/README.md @@ -97,15 +97,12 @@ Result ``` > node 1.js ############## fruit banana ############## -node1 received: banana node2 received: banana node3 received: banana ############## fruit apple ############## -node1 received: apple node2 received: apple node3 received: apple ############## fruit car ############## -node1 received: car ############## fruit orange ############## node1 received: orange node2 received: orange