David Dias 6905f1ba41
feat: (BREAKING CHANGE) overhaul libp2p config and constructor
* docs: update chat example and add info to its readme
* docs: update echo example
* docs: update libp2p in browser example
* docs: update pubsub example
* docs: update peer and content routing examples
* docs: update discovery mechanisms example
* docs: update encrypted comms example
* docs: update protocol and stream muxing example
* feat: add config validation
* test: update CI configs, use only node 8
2018-06-28 10:06:25 +02:00
..

Publish Subscribe

Publish Subscribe is also included on the stack. Currently, we have on PubSub implementation which we ship by default libp2p-floodsub, with many more being researched at research-pubsub.

We've seen many interesting use cases appear with this, here are some highlights:

1. Setting up a simple PubSub network on top of libp2p

For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in Discovery Mechanisms. You can find the complete version at 1.js.

Using PubSub is super simple, all you have to do is start a libp2p node, PubSub will be enabled by default.

series([
  (cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)),
  (cb) => setTimeout(cb, 500)
], (err) => {
  if (err) { throw err }

  // Subscribe to the topic 'news'
  node1.pubsub.subscribe('news',
    (msg) => console.log(msg.from, msg.data.toString()),
    () => {
      setInterval(() => {
        // Publish the message on topic 'news'
        node2.pubsub.publish(
          'news',
          Buffer.from('Bird bird bird, bird is the word!'),
          () => {}
        )
      }, 1000)
    }
  )
})

The output of the program should look like:

> node 1.js
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 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. Here is a list of some of the exciting examples: