Created
December 28, 2019 12:34
-
-
Save vasa-develop/c4de0baef9414f2993d577a418887f0d to your computer and use it in GitHub Desktop.
SimpleAsWater: Building a Chat Application using Libp2p
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
'use strict' | |
/* eslint-disable no-console */ | |
const PeerId = require('peer-id') | |
const PeerInfo = require('peer-info') | |
const Node = require('./libp2p_bundle') | |
const async = require('async') | |
const chalk = require('chalk'); | |
const emoji = require('node-emoji') | |
let moonPeerId | |
async.parallel([ | |
(callback) => { | |
PeerId.createFromJSON(require('./ids/earthId'), (err, earthPeerId) => { | |
if (err) { | |
throw err | |
} | |
callback(null, earthPeerId) | |
}) | |
}, | |
(callback) => { | |
PeerId.createFromJSON(require('./ids/moonId'), (err, moonPeerId) => { | |
if (err) { | |
throw err | |
} | |
callback(null, moonPeerId) | |
}) | |
} | |
], (err, ids) => { | |
if (err) throw err | |
const earthPeerInfo = new PeerInfo(ids[0]) | |
earthPeerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0') | |
const nodeDialer = new Node({ peerInfo: earthPeerInfo }) | |
const moonPeerInfo = new PeerInfo(ids[1]) | |
moonPeerId = ids[1] | |
moonPeerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/10333') | |
nodeDialer.start((err) => { | |
if (err) { | |
throw err | |
} | |
console.log(emoji.get('large_blue_circle'), chalk.blue(' Earth Ready '), | |
emoji.get('headphones'), chalk.blue(' Listening on: ')); | |
nodeDialer.dialProtocol(moonPeerInfo, '/chat/1.0.0', (err, conn) => { | |
if (err) { | |
throw err | |
} | |
console.log('\n' + emoji.get('large_blue_circle'), | |
chalk.blue(' Earth dialed to Moon on protocol: /chat/1.0.0')); | |
console.log(`${emoji.get('incoming_envelope')} | |
${chalk.bold(`Type a message and press enter. See what happens...`)}`) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment