Skip to content

Instantly share code, notes, and snippets.

@modulitos
Last active April 18, 2016 00:44
Show Gist options
  • Save modulitos/1926f4f4a2ddfbf506263bf7cc4fbcad to your computer and use it in GitHub Desktop.
Save modulitos/1926f4f4a2ddfbf506263bf7cc4fbcad to your computer and use it in GitHub Desktop.
  • first.js & second.js are in separate directories
  • run node first.js
  • copy key that is logged
  • run node second.js <key>
// var swarm = require('discovery-swarm')
// var sw = swarm({
// dns: {
// server: [
// 'discovery1.publicbits.org',
// 'discovery2.publicbits.org'
// ]
// },
// dht: true
// })
var net = require('net')
console.log("starting server...")
var level = require('level')
var db = level('./hypercore.db')
hypercore = require('hypercore')
var core = hypercore(db) // db is a leveldb instance
// var feed = core.createFeed("1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e")
core.list(function(err, list) {
console.log("first.core.list: list:", list)
var key = !list.length ? null : list[0]
var feed = core.createFeed(key, { secretKey: Buffer('1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e') })
// var feed = core.createFeed({ key: 'myKey' })
console.log('first: feed.opened:', feed.opened)
console.log('first: feed.key:', feed.key)
console.log('first: feed.secretKey:', feed.secretKey)
console.log('first: feed.publicId:', feed.publicId)
feed.append(['poop1', 'world1'], function () {
console.log('first: we have appended poop1 world1')
console.log('first: key is', feed.key.toString('hex'))
})
feed.on('upload', function (block, data) {
console.log('first: uploaded block', block, data)
console.log('first: uploaded data.toString:', data.toString())
})
feed.on('error', function (block, data) {
console.log('first: error detected:', block, data)
})
feed.on('download', function (block, data) {
console.log('first: downloaded block', block, data)
console.log('first: downloaded data.toString:', data.toString())
})
// // sw.listen(1000)
// sw.listen(3333)
// // sw.join('pizza')
console.log('first: channel key:', feed.key.toString('hex'))
// sw.join('lukeluke')
// sw.on('connection', function (connection) {
// console.log('first: found + connected to peer')
// // var replicate = feed.replicate()
// connection.pipe(feed.replicate()).pipe(connection)
// })
// hypercore npmjs example:
var server = net.createServer(function (socket) {
console.log('found + connected to peer')
socket.pipe(feed.replicate()).pipe(socket)
})
server.listen(3333)
})
// var swarm = require('discovery-swarm')
// var sw = swarm({
// dns: {
// server: [
// 'discovery1.publicbits.org',
// 'discovery2.publicbits.org'
// ]
// },
// dht: true
// })
var net = require('net')
console.log("starting server...")
var level = require('level')
var db = level('./hypercore.db')
hypercore = require('hypercore')
var core = hypercore(db) // db is a leveldb instance
var key = process.argv[2]
// var key = '1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e'
// var key = 'b6fc0cfd572038f860710438a64f6b58225e5ff61e9f1a2fea1489bbce7e0a93'
var feed = core.createFeed(key, { secretKey: Buffer('1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e1e2e3e4e5e6e7e8e') })
console.log('second: feed.live:', feed.live)
console.log('second: feed.opened:', feed.opened)
console.log('second: feed.key:', feed.key)
console.log('second: feed.secretKey:', feed.secretKey)
console.log('second: feed.publicId:', feed.publicId)
feed.append(['poop2', 'world2'], function () {
console.log('second: we have appended poop2 world2')
console.log('second: key is', feed.key.toString('hex'))
})
feed.on('upload', function (block, data) {
console.log('second: uploaded block', block, data)
console.log('second: uploaded data.toString:', data.toString())
})
feed.on('error', function (block, data) {
console.log('second: error detected:', block, data)
})
feed.on('download', function (block, data) {
console.log('second: downloaded block', block, data)
console.log('second: downloaded data.toString:', data.toString())
})
// // sw.listen(1000)
// sw.listen(3334)
// // sw.join('pizza')
// console.log('second: feed key', Buffer.from(key, 'hex').toString('hex'))
// console.log('second: joining swarm', key)
// sw.join('lukeluke')
// sw.on('connection', function (connection) {
// console.log('found + connected to peer')
// connection.pipe(feed.replicate()).pipe(connection)
// })
// hypercore npmjs example:
var server = net.createServer(function (socket) {
console.log('found + connected to peer')
socket.pipe(feed.replicate()).pipe(socket)
})
server.listen(3334)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment