Skip to content

Commit f7e1426

Browse files
committed
chore: update pubsub example by disabled emit self (#823)
1 parent 7d76ba1 commit f7e1426

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

examples/pubsub/1.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const createNode = async () => {
4343
})
4444
await node1.pubsub.subscribe(topic)
4545

46+
// Will not receive own published messages by default
4647
node2.pubsub.on(topic, (msg) => {
4748
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
4849
})

examples/pubsub/README.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ const node2 = nodes[1]
4444

4545
// Add node's 2 data to the PeerStore
4646
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
47-
4847
await node1.dial(node2.peerId)
4948

5049
node1.pubsub.on(topic, (msg) => {
5150
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
5251
})
5352
await node1.pubsub.subscribe(topic)
5453

54+
// Will not receive own published messages by default
5555
node2.pubsub.on(topic, (msg) => {
5656
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
5757
})
@@ -68,25 +68,34 @@ The output of the program should look like:
6868
```
6969
> node 1.js
7070
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
71-
node2 received: Bird bird bird, bird is the word!
7271
node1 received: Bird bird bird, bird is the word!
73-
node2 received: Bird bird bird, bird is the word!
7472
node1 received: Bird bird bird, bird is the word!
7573
```
7674

77-
You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages.
75+
You can change the pubsub `emitSelf` option if you want the publishing node to receive its own messages.
7876

7977
```JavaScript
8078
const defaults = {
8179
config: {
8280
pubsub: {
8381
enabled: true,
84-
emitSelf: false
82+
emitSelf: true
8583
}
8684
}
8785
}
8886
```
8987

88+
The output of the program should look like:
89+
90+
```
91+
> node 1.js
92+
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
93+
node1 received: Bird bird bird, bird is the word!
94+
node2 received: Bird bird bird, bird is the word!
95+
node1 received: Bird bird bird, bird is the word!
96+
node2 received: Bird bird bird, bird is the word!
97+
```
98+
9099
## 2. Future work
91100

92101
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:

examples/pubsub/message-filtering/1.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const createNode = async () => {
4444

4545
//subscribe
4646
node1.pubsub.on(topic, (msg) => {
47+
// Will not receive own published messages by default
4748
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
4849
})
4950
await node1.pubsub.subscribe(topic)

examples/pubsub/message-filtering/README.md

-3
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,12 @@ Result
9797
```
9898
> node 1.js
9999
############## fruit banana ##############
100-
node1 received: banana
101100
node2 received: banana
102101
node3 received: banana
103102
############## fruit apple ##############
104-
node1 received: apple
105103
node2 received: apple
106104
node3 received: apple
107105
############## fruit car ##############
108-
node1 received: car
109106
############## fruit orange ##############
110107
node1 received: orange
111108
node2 received: orange

0 commit comments

Comments
 (0)