|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>IPFS in the Browser</title> |
| 5 | + <script src="https://unpkg.com/ipfs/dist/index.min.js"></script> |
| 6 | + <script type="text/javascript"> |
| 7 | + // We provide a hosted signalling endpoint that you can use to discover |
| 8 | + // and dial to other nodes. It is hosted at `star-signal.cloud.ipfs.team` |
| 9 | + // If you run your own signalling, you can change this multiaddr. |
| 10 | + const SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/' |
| 11 | + |
| 12 | + const repoPath = 'ipfs-' + Math.random() |
| 13 | + |
| 14 | + // Create an IPFS node |
| 15 | + const node = new Ipfs({ |
| 16 | + repo: repoPath |
| 17 | + }) |
| 18 | + |
| 19 | + // Init the node |
| 20 | + node.init(handleInit) |
| 21 | + |
| 22 | + function handleInit (err) { |
| 23 | + if (!err) { // The repo was initialized for the first time, we need to configure it |
| 24 | + addWebRTCMultiaddr() |
| 25 | + } else if (err && err.message !== 'repo already exists') { // The repo already existed, let's just load it |
| 26 | + loadRepo() |
| 27 | + } else { |
| 28 | + throw err |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + function addWebRTCMultiaddr() { |
| 33 | + // Addj the WebrTCStar Multiaddr to your node |
| 34 | + node.config.get(function (err, config) { |
| 35 | + if (err) { |
| 36 | + throw err |
| 37 | + } |
| 38 | + |
| 39 | + const starAddr = (SIGNALING_SERVER + config.Identity.PeerID) |
| 40 | + |
| 41 | + node.config.set('Addresses.Swarm[1]', starAddr, loadRepo) |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + function loadRepo() { |
| 46 | + node.load(() => node.goOnline(() => { |
| 47 | + console.log('Online status: ', node.isOnline() ? 'online' : 'offline') |
| 48 | + |
| 49 | + document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline') |
| 50 | + |
| 51 | + // \o/ Now you have an IPFS node using WebRTC to find other nodes! |
| 52 | + // You can write more code here to use it. Use methods like |
| 53 | + // node.files.add, node.files.get. See the API docs here: |
| 54 | + // https://github.com/ipfs/interface-ipfs-core/tree/master/API |
| 55 | + })) |
| 56 | + } |
| 57 | + </script> |
| 58 | +</head> |
| 59 | +<body> |
| 60 | + <h1>IPFS in the Browser</h1> |
| 61 | + <p>This page creates an IPFS node in your browser and drops it into the global Javascript namespace as <b><em style="background-color:#d7d6d6">node</em></b>. Open the console to play around with it.</p> |
| 62 | + <p>Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.</p> |
| 63 | + <div id="status">Node status: offline</div> |
| 64 | + |
| 65 | + <h2>Some suggestions</h2> |
| 66 | + |
| 67 | + <p>Try adding a new file:</p> |
| 68 | + |
| 69 | + <code style="display:block; white-space:pre-wrap; background-color:#d7d6d6"> |
| 70 | + node.files.add(new node.types.Buffer('Hello world!'), function (err, res) { |
| 71 | + if (err || !res) { |
| 72 | + return console.error('Error - ipfs files add', err, res) |
| 73 | + } |
| 74 | + |
| 75 | + res.forEach(function (file) { |
| 76 | + console.log('successfully stored', file) |
| 77 | + }) |
| 78 | + }) |
| 79 | + </code> |
| 80 | + |
| 81 | + <p>You can cat that same file. If you used the exact same string as above ('Hello world!') you should have an hash like this: 'QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY'</p> |
| 82 | + |
| 83 | + <code style="display:block; white-space:pre-wrap; background-color:#d7d6d6"> |
| 84 | + node.files.cat('QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY', function (err, stream) { |
| 85 | + var res = '' |
| 86 | + |
| 87 | + stream.on('data', function (chunk) { |
| 88 | + res += chunk.toString() |
| 89 | + }) |
| 90 | + |
| 91 | + stream.on('error', function (err) { |
| 92 | + console.error('Error - ipfs files cat ', err) |
| 93 | + }) |
| 94 | + |
| 95 | + stream.on('end', function () { |
| 96 | + console.log('Got:', res) |
| 97 | + }) |
| 98 | + }) |
| 99 | + </code> |
| 100 | +</body> |
| 101 | +</html> |
0 commit comments