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
var discovery = require('discovery-swarm')
var hypercore = require('hypercore')
var pump = require('pump')
var feed = hypercore('./single-chat-feed-clone', '{paste the public key from the prev exercise}', {
valueEncoding: 'json'
})
feed.createReadStream({ live: true})
.on('data', function (data) {
console.log(data)
})
var swarm = discovery()
feed.ready(function () {
// we use the discovery as the topic
swarm.join(feed.discoveryKey)
swarm.on('connection', function (connection) {
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(connection, feed.replicate({ live: true }), connection)
})
})
The hypercore API feed.replicate({live: true})
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 single-chat-viewer.js
will join the swarm using the chat feeds discovery key
and use that to find other chat peers.
Extend single-chat.js
with swarm code similar to the 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 two terminals.
Once you solve this exercise continue to exercise 8