Created
October 20, 2022 00:14
-
-
Save ninabreznik/6efd43db6f369c48f7d451cc3e014c2a to your computer and use it in GitHub Desktop.
Feed replication example with 2 swarms and 2 peers
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 RAM = require('random-access-memory') | |
const hyperswarm = require('hyperswarm') | |
const createTestnet = require('@hyperswarm/testnet') | |
// const bootstrap = require('./bootstrappers.js') | |
start() | |
async function start () { | |
const { bootstrap } = await createTestnet(3) | |
const feed1 = new hypercore(RAM) | |
await feed1.ready() | |
await feed1.append(Buffer.from('hello world')) | |
await feed1.append(Buffer.from('hallo welt')) | |
await feed1.append(Buffer.from('pozdravljen svet')) | |
await feed1.append(Buffer.from('zdravo svete')) | |
await feed1.append(Buffer.from('juhuhu')) | |
await feed1.append(Buffer.from('halli hallo')) | |
await feed1.append(Buffer.from('hiya')) | |
const foo = await feed1.get(0) | |
console.log({foo}) | |
const feed2 = new hypercore(RAM, feed1.key) | |
await feed2.ready() | |
1 | |
const swarm1 = new hyperswarm({ bootstrap }) | |
const swarm2 = new hyperswarm({ bootstrap }) | |
swarm1.on('connection', (conn, info) => { | |
console.log('new connection swarm1') | |
conn.on('error', onerror) | |
conn.pipe(feed1.replicate(conn.isInitiator)).pipe(conn) | |
// conn.end() | |
}) | |
swarm2.on('connection', async (conn, info) => { | |
console.log('new connection swarm2') | |
conn.on('error', onerror) | |
conn.pipe(feed2.replicate(conn.isInitiator)).pipe(conn) | |
await new Promise(ok => setTimeout(ok, 1000)) | |
console.log('getting data') | |
const data = await feed2.get(0) | |
console.log({data}) | |
}) | |
const topic = feed1.discoveryKey | |
await swarm1.join(topic, { server: true, client: true }).flushed() | |
swarm2.join(topic, { client: true, server: true }) | |
await swarm2.flush() | |
// await swarm1.destroy() | |
// await swarm2.destroy() | |
function onerror (err) { console.error({err})} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment