8: Multiple peers in one app!

In review so far we know how to:

  1. create a secure append-only log, that a single user can write to
  2. read messages from this hypercore
  3. replicate one of these hypercores to another, remote hypercore

There is a major gap in our code though if we'd like to make a chat program: with hypercore, there can only be one writer!

Well, what if each user had their own hypercore? Then we could view their contents together to see all of the chat history as other people are writing.

This was the motivation behind multifeed, a module that lets you operate on a set of hypercores, and create a replication stream, in the same way that you replicated hypercores in the last exercise.

Exercise

Make a copy of single-chat.js called multichat.js, and replace usage of hypercore with multifeed. Here is how to create a multifeed, a new writer, and write to that feed:

const hypercore = require('hypercore')
const multifeed = require('multifeed')

const multi = multifeed('./multichat', {valueEncoding:'json'})

multi.writer('local', function (err, feed) {
  // TODO: join swarm

  // 'feed' is a hypercore, just like before
  // except now we can manage many of them together!
  feed.append({
    type: 'chat-message',
    nickname: 'cat-lover',
    text: 'hello world',
    timestamp: '2018-11-05T14:26:000Z' // new Date().toISOString()
  }, function (err, seq) {
    if (err) throw err
    console.log('Data was appended as entry #' + seq)
  })
})

See if you can also add peer discovery and replication to this. Just like hypercore, multifeed has a multi.replicate() API that lets two peers sync their data over a duplex stream. Look back at how you set you peer discovery for hypercore for ideas on how to do this.

Tricky bits

const topic = crypto.createHash('sha256')
            .update('foobar-123')
            .digest()

swarm.join(topic)
const suffix = process.argv[2]

const multi = multifeed('./multichat-' + suffix, { valueEncoding:'json' })

This way, you can run node multichat.js 1 in one window, and node multichat.js 2 in the other window. If you try to run two processes that open the same multifeed database, things won't work!

Tips

Once you solve this exercise continue to exercise 9