chore: update pubsub example by disabled emit self (#823)

This commit is contained in:
Vasco Santos 2020-12-04 14:53:05 +01:00
parent 7461d2a642
commit bbdaae17ae
4 changed files with 16 additions and 8 deletions

View File

@ -43,6 +43,7 @@ const createNode = async () => {
}) })
await node1.pubsub.subscribe(topic) await node1.pubsub.subscribe(topic)
// Will not receive own published messages by default
node2.pubsub.on(topic, (msg) => { node2.pubsub.on(topic, (msg) => {
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
}) })

View File

@ -44,7 +44,6 @@ const node2 = nodes[1]
// Add node's 2 data to the PeerStore // Add node's 2 data to the PeerStore
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs) node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
await node1.dial(node2.peerId) await node1.dial(node2.peerId)
node1.pubsub.on(topic, (msg) => { node1.pubsub.on(topic, (msg) => {
@ -52,6 +51,7 @@ node1.pubsub.on(topic, (msg) => {
}) })
await node1.pubsub.subscribe(topic) await node1.pubsub.subscribe(topic)
// Will not receive own published messages by default
node2.pubsub.on(topic, (msg) => { node2.pubsub.on(topic, (msg) => {
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
}) })
@ -68,25 +68,34 @@ The output of the program should look like:
``` ```
> node 1.js > node 1.js
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
node2 received: Bird bird bird, bird is the word!
node1 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! 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 ```JavaScript
const defaults = { const defaults = {
config: { config: {
pubsub: { pubsub: {
enabled: true, 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 ## 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: 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:

View File

@ -44,6 +44,7 @@ const createNode = async () => {
//subscribe //subscribe
node1.pubsub.on(topic, (msg) => { node1.pubsub.on(topic, (msg) => {
// Will not receive own published messages by default
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`) console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
}) })
await node1.pubsub.subscribe(topic) await node1.pubsub.subscribe(topic)

View File

@ -97,15 +97,12 @@ Result
``` ```
> node 1.js > node 1.js
############## fruit banana ############## ############## fruit banana ##############
node1 received: banana
node2 received: banana node2 received: banana
node3 received: banana node3 received: banana
############## fruit apple ############## ############## fruit apple ##############
node1 received: apple
node2 received: apple node2 received: apple
node3 received: apple node3 received: apple
############## fruit car ############## ############## fruit car ##############
node1 received: car
############## fruit orange ############## ############## fruit orange ##############
node1 received: orange node1 received: orange
node2 received: orange node2 received: orange