7b - Replication

We can use the things we learned about hypercore keys and swarms to start sharing our chat feed with other people now. Since only the process with the secret key can write to the chat feed we wanna make a simple chat viewer that will just print out replicated chat messages.

Copy the following snippet and save it as single-chat-viewer.js

const Hyperswarm = require('hyperswarm')
const hypercore = require('hypercore')
const pump = require('pump')

const feed = hypercore('./single-chat-feed-clone', '{paste the public key created by single-chat.js}', {
  valueEncoding: 'json'
})

feed.createReadStream({ live: true })
  .on('data', function (data) {
    console.log(data)
  })

const swarm = new Hyperswarm()

feed.ready(function () {
  // we use the discovery as the topic
  swarm.join(feed.discoveryKey)
  swarm.on('connection', function (socket, info) {
    console.log('(New peer connected!)')

    // We use the pump module instead of stream.pipe(otherStream)
    // as it does stream error handling, so we do not have to do that
    // manually.

    // See below for more detail on how this work.
    pump(socket, feed.replicate(info.client, { live: true }), socket)
  })
})

The hypercore API feed.replicate(isInitiator, opts) returns a Duplex stream, a special type of node stream that can be both read from and written to. If you pump the feed replication streams together, they automatically synchronize themselves so that they both end up with the same data on each side.

The isInitiator parameter is used to set up the encryption protocol between peers, which requires one side to designate themselves as the connection initiator. The only thing to worry about here is that one side sets this to true and the other side to false.

The single-chat-viewer.js will join the swarm using the chat feed's discovery key and use that to find other chat peers. However, it won't work if you run it now since you haven't told single-chat.js to join the swarm yet. Let's do that next.

Exercise

Extend single-chat.js with swarm code similar to the single-chat-viewer.js above so that it joins the discovery swarm using the discovery key and pumps the replication streams together.

Running both single-chat.js and single-chat-viewer.js should live replicate the messages between the two terminals.

Tips

More Resources

Once you solve this exercise continue to exercise 8