Created
August 18, 2022 07:56
-
-
Save Nuhvi/a3f1d24b597913303afcb6c568f4b042 to your computer and use it in GitHub Desktop.
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
import Hyperswarm from 'hyperswarm' | |
import Corestore from 'corestore' | |
import ProtomuxRPC from 'protomux-rpc' | |
import RAM from 'random-access-memory' | |
import createTestnet from '@hyperswarm/testnet' | |
const testnet = await createTestnet() | |
const swarm_A = new Hyperswarm(testnet) | |
const store_A = new Corestore(RAM) | |
swarm_A.on('connection', stream => { | |
console.log('swarm A connection callback') | |
store_A.replicate(stream) | |
const rpc = new ProtomuxRPC(stream) | |
rpc.respond('echo', req => req) | |
}) | |
await swarm_A.listen() | |
const swarm_B = new Hyperswarm(testnet) | |
const store_B = new Corestore(RAM) | |
swarm_B.on('connection', async stream => { | |
console.log('swarm B connection callback') | |
store_B.replicate(stream) | |
const rpc = new ProtomuxRPC(stream) | |
const response = await rpc.request('echo', Buffer.from('foo')) | |
console.log({ response }) | |
}) | |
swarm_B.joinPeer(swarm_A.keyPair.publicKey) | |
Author
Nuhvi
commented
Aug 19, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment