Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
kappa
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
You can’t perform that action at this time.