Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active July 10, 2019 00:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwblair/dbde67c21cf7590e965942d2ac17278c to your computer and use it in GitHub Desktop.
Save dwblair/dbde67c21cf7590e965942d2ac17278c to your computer and use it in GitHub Desktop.
Testing out substack's "ACK' functionality for hypercore!

testing out ACKs in hypercore

Using substack's 'ack' branch of hypercore, you can set an 'ack' flag to 'true' when creating a hypercore replication feed, and you'll be able to tell, for any given hypercore log block you append to a hypercore feed, whether it has been received by a peer.

As a test, I ran "node testsend.js" below ... which generates a public key [KEY], and allows me to type messages into the terminal 'live'. In another directory, I ran "node testreceive.js [KEY]" in order receive those messages ...

Here's what it looked like on the 'send' side as I typed in new messages (I'm including a snippet after I'd already been testing a while) ...

2019-07-06T23:03:14.746Z> bubbles
2018-11-05T14:26:000Z> hello world
9
test another
2019-07-06T23:04:12.726Z> test another
10
hear hear!
2019-07-06T23:04:27.795Z> hear hear!
11
yay it's working!
2019-07-06T23:05:08.945Z> yay it's working!
12

You can see that the hyperlog replication feed block numbers ('9', '10', '11', '12') are being logged to the console as I send the messages, indicating that a peer has received those blocks as I append them.

COOL.

The code I used for 'testsend' and 'testreceive' are below.

var discovery = require('discovery-swarm')
var hypercore = require('hypercore')
var pump = require('pump')
if( process.argv.length != 3 ) {
console.log( "Usage" )
console.log( "node dump.js cabalkey")
process.exit(1)
}
// Strip out the awesome
const key = process.argv[2].replace('cabal://', '').replace('cbl://', '').replace('dat://', '').replace(/\//g, '')
//const key = 'c4a2a72e2df7867a4d9d0eeaf98e2930d6c3948f0c9087c34a09a2bcfbd69615';
console.log( "Connecting to", key)
// var feed = hypercore('./single-chat-feed-clone', '{paste the public key from the prev exercise}', {
var feed = hypercore('./received', key, {
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.
var stream=feed.replicate({ live:true,ack:true})
// See below for more detail on how this work.
pump(connection, stream, connection)
})
})
var discovery = require('discovery-swarm')
var hypercore = require('hypercore')
var pump = require('pump')
var feed = hypercore('sending', {
valueEncoding: 'json'
})
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)
})
// feed.get(0, function (err, msg) {
// console.log('msg', msg)
// })
process.stdin.on('data', function (data) {
feed.append({
type: 'chat-message',
nickname: 'cat-lover',
text: data.toString(),
timestamp: new Date().toISOString()
})
})
feed.createReadStream({live:true})
.on('data', function (data) {
console.log(data.timestamp + '> ' + data.text.trim())
})
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.
var stream=feed.replicate({ live:true,ack:true})
// See below for more detail on how this work.
// pump(connection, feed.replicate({ live: true, ack:true}), connection)
pump(connection, stream, connection)
stream.on('ack', function (block) {
console.log(block) // block is a sequence number
})
})
console.log('public key:', feed.key.toString('hex'))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment