Last active
December 11, 2019 05:00
-
-
Save bellbind/aded5a1c27fcabc5b47de41381e665fb to your computer and use it in GitHub Desktop.
[IPFS][libp2p][browser] Pubsub example on libp2p peer in IPFS node
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
<!doctype html> | |
<html> | |
<head> | |
<title>Pubsub on libp2p</title> | |
<script type="module" src="./main.js"></script> | |
</head> | |
<body> | |
<h3 id="myid"></h3> | |
<div> | |
id: <input id="id" type="text" size="40"/> | |
<button id="chain">chain</button> | |
</div> | |
<div> | |
message: <input id="msg" type="text" size="40"/> | |
<button id="send">send</button> | |
</div> | |
<hr/> | |
<pre id="log"></pre> | |
</body> | |
</html> |
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
// this bundle maybe non ES-module, but it can import to load as `window.Ipfs` | |
// (js-ipfs 0.39.0) | |
import "https://cdn.jsdelivr.net/npm/ipfs/dist/index.js"; | |
//console.log(window.Ipfs); | |
const promisify = func => function (...args) { | |
return new Promise((f, e) => func.call( | |
this, ...args, (err, value) => err ? e(err) : f(value))); | |
}; | |
const main = async () => { | |
const node = new Ipfs({ | |
repo: `ipfs-${Math.random()}`, | |
relay: {enabled: true, hop: {enabled: true, active: true}}, | |
//EXPERIMENTAL: {pubsub: true}, | |
//config: {Pubsub: {Enabled: true}}, // default from 0.40.0 | |
//libp2p: {config: {pubsub: {enabled: true},}}, | |
}); | |
await node.ready; | |
console.log("IPFS version:", (await node.version()).version); | |
console.log(`Peer ID:`, (await node.id()).id); | |
const myid = node.libp2p.peerInfo.id.toB58String(); | |
console.log("libp2p ID:", myid); | |
for (const ma of node.libp2p.peerInfo.multiaddrs.toArray()) { | |
console.log("multiaddr:", ma.toString()); | |
} | |
document.querySelector("#chain").addEventListener("click", ev => { | |
(async () => { | |
const id = document.querySelector("#id").value; | |
const p2pid = `/p2p-circuit/ipfs/${id}`; | |
//NOTE: For pubsub, directly chained pubsub-enabed peers each other | |
await node.swarm.connect(p2pid); | |
document.querySelector("#id").value = ""; | |
document.querySelector("#log").prepend(`(Swarm connected: ${id})\n`); | |
})().catch(console.error); | |
}); | |
const topicId = "pubsub-example-topic"; | |
const handle = msg => { | |
//console.log(msg); | |
const text = new TextDecoder().decode(msg.data); | |
document.querySelector("#log").prepend(`${text}\n`); | |
}; | |
await node.libp2p.pubsub.subscribe(topicId, handle); | |
console.log("subscribed"); | |
document.querySelector("#send").addEventListener("click", ev => { | |
(async () => { | |
const msg = document.querySelector("#msg").value; | |
const text = `${myid}: ${msg}`; | |
await node.libp2p.pubsub.publish( | |
topicId, new TextEncoder().encode(text)); | |
document.querySelector("#msg").value = ""; | |
})().catch(console.error); | |
}); | |
document.querySelector("#myid").textContent = myid; | |
}; | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: Pubsub feature(IPFS-0.40.0: libp2p-gossipsub) used for IPFS is not always reached messages to every peers. Several peers sometimes dropped messages.