Last active
November 15, 2018 12:27
-
-
Save sorenlouv/83f9e5b8f40c5fa0bc4dd4cafe6c0f2b to your computer and use it in GitHub Desktop.
kappa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hypercore = require('hypercore'); | |
const discovery = require('discovery-swarm'); | |
const multifeed = require('multifeed'); | |
const pump = require('pump'); | |
const suffix = process.argv[2]; | |
const db = `./multichat-${suffix}`; | |
console.log(`Using db: ${db}`); | |
const multi = multifeed(hypercore, db, { valueEncoding: 'json' }); | |
multi.on('feed', function (feed, name) { | |
feed.createReadStream({ live: true }).on('data', function(data) { | |
console.log(`${name} ${data.timestamp} ${data.nickname}: ${data.text}`); | |
}); | |
}) | |
multi.writer('local', function(err, feed) { | |
// find and connect to swarm | |
const swarm = discovery(); | |
feed.ready(function() { | |
swarm.join('lolzland'); | |
swarm.on('connection', function(connection) { | |
console.log('(New peer connected!)'); | |
pump(connection, multi.replicate({ live: true }), connection); | |
}); | |
}); | |
// send messages on input | |
process.stdin.on('data', data => { | |
feed.append({ | |
type: 'chat-message', | |
nickname: `Bruger ${suffix}`, | |
text: data.toString().trim(), | |
timestamp: new Date().toISOString() | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment