Skip to content

Commit

Permalink
chore: update pubsub example by disabled emit self (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 10, 2020
1 parent 7461d2a commit bbdaae1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/pubsub/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`)
})
Expand Down
19 changes: 14 additions & 5 deletions examples/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ 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) => {
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
})
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)}`)
})
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions examples/pubsub/message-filtering/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions examples/pubsub/message-filtering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bbdaae1

Please sign in to comment.