Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit e916a5d

Browse files
committed
docs(examples): Interop Example 1
Move interop-example1 to examples/access-go-ipfs-files/cat-a-file. Remove dependency on ipfs-daemon and use ipfs.js. Add start-ipfs function to handle the init dance. Update instructions in README.
1 parent aac4dec commit e916a5d

File tree

8 files changed

+189
-143
lines changed

8 files changed

+189
-143
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
public/ipfs.js
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# access-go-ipfs-files - cat-a-file
2+
3+
**WIP**
4+
5+
## TODO
6+
7+
- Add "connect to peer" input field and "connect" button under the "Peers" section in index.html
8+
- Add `connectToPeer` function which calls `ipfs.swarm.connect(multiaddr)`. See https://github.com/ipfs/js-ipfs/blob/b0a7cd83cbf146b0f147467dedc686f94a5f751f/examples/ipfm/src/DataStore.js#L82 and https://github.com/ipfs/js-ipfs/blob/b0a7cd83cbf146b0f147467dedc686f94a5f751f/examples/ipfm/README.md#start-an-interplanetary-file-exchange-daemon. The multiaddr to connect to looks like this: `/ip4/127.0.0.1/tcp/9999/ws/ipfs/QmZGH8GeASSkSZoNLPEBu1MqtzLTERNUEwh9yTHLEF5kcW`
9+
- Hook up "connect" button's click event to `connectToPeer` function
10+
- Add instructions to this README on how to add a file in go-ipfs
11+
- Add instructions to this README on how to cat it in the UI
12+
- Make sure the "Start a go-ipfs daemon" instructions are correct
13+
- Make sure go-ipfs daemon and the example connect to each other
14+
15+
## Step-by-step Instructions
16+
17+
### Start a go-ipfs daemon
18+
19+
1. Install go-ipfs from master (TODO: link).
20+
21+
2. Run `ipfs init`
22+
23+
3. Edit your IPFS config file, located at `~/.ipfs/config`
24+
25+
4. Add a Websocket listener address to `Addresses.Swarm`. It should look like this after editing:
26+
```
27+
"Addresses": {
28+
"API": "/ip4/127.0.0.1/tcp/5001",
29+
"Gateway": "/ip4/0.0.0.0/tcp/8080",
30+
"Swarm": [
31+
"/ip4/0.0.0.0/tcp/4001",
32+
"/ip4/0.0.0.0/tcp/9999/ws"
33+
]
34+
},
35+
```
36+
37+
5. Start the go-ipfs daemon with:
38+
```
39+
ipfs daemon
40+
```
41+
42+
6. You should see the Websocket address in the output:
43+
```
44+
Initializing daemon...
45+
Swarm listening on /ip4/127.0.0.1/tcp/4001
46+
Swarm listening on /ip4/127.0.0.1/tcp/9999/ws
47+
Swarm listening on /ip4/192.168.10.38/tcp/4001
48+
Swarm listening on /ip4/192.168.10.38/tcp/9999/ws
49+
API server listening on /ip4/127.0.0.1/tcp/5001
50+
Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8080
51+
Daemon is ready
52+
```
53+
54+
If you see address like `Swarm listening on /ip4/127.0.0.1/tcp/9999/ws`, it means all good!
55+
56+
## Start the example
57+
58+
**NOTE!** Before running the examples, you need to build `js-ipfs`. You can do this by following the instructions in https://github.com/ipfs/js-ipfs#clone-and-install-dependnecies and then building it as per https://github.com/ipfs/js-ipfs#build-a-dist-version.
59+
60+
```
61+
npm install
62+
npm start
63+
```
64+
65+
Open http://127.0.0.1:8080 in a browser.
66+
67+
**TODO: add instructions how to cat a hash in the UI.**
68+
69+
## Tutorial
70+
71+
Steps
72+
1. create IPFS instance
73+
74+
TODO. See `./start-ipfs.js`
75+
76+
3. add a file in go-ipfs
77+
4. copy file's hash
78+
5. ipfs.files.cat
79+
80+
TODO. add ipfs.files.cat code examples from index.html
81+
82+
6. output the buffer to <img>
83+
84+
```
85+
...
86+
stream.on('end', () => {
87+
const blob = new Blob(buf)
88+
picture.src = URL.createObjectURL(blob)
89+
})
90+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "cat-a-file",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"postinstall": "cp ../../../dist/index.js ./public/ipfs.js",
7+
"start": "http-server -c-1"
8+
},
9+
"author": "Haad",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"http-server": "^0.9.0"
13+
}
14+
}

examples/interop-examples/index.html renamed to examples/access-go-ipfs-files/cat-a-file/public/index.html

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<meta charset="utf-8">
44
<link rel="stylesheet" href="styles.css">
55
<!-- Include IPFS -->
6-
<script type="text/javascript" src="node_modules/ipfs-daemon/dist/ipfs-browser-daemon.js" charset="utf-8"></script>
6+
<script src="ipfs.js"></script>
7+
<!-- Include a helper script that starts IPFS -->
8+
<script type="text/javascript" src="start-ipfs.js" charset="utf-8"></script>
79
</head>
810

911
<body>
@@ -56,34 +58,45 @@ <h2>Files</h2>
5658
const multihashInput = document.getElementById("multihash")
5759
const catButton = document.getElementById("cat")
5860

59-
let ipfs, pollPeersTimer
61+
let ipfs, peerInfo, pollPeersTimer
62+
63+
const ipfsOptions = {
64+
// Directory to which save IPFS data to
65+
IpfsDataDir: dirInput.value,
66+
// IPFS dev server: webrtc-star-signalling.cloud.ipfs.team
67+
SignalServer: signalServerInput.value,
68+
// Local webrtc-star server, you can get it from:
69+
// https://github.com/libp2p/js-libp2p-webrtc-star
70+
// SignalServer: '127.0.0.1:9090',
71+
}
6072

6173
// Start IPFS instance
6274
const start = () => {
6375
if (!ipfs) {
64-
// Create an IPFS instance
65-
ipfs = new IpfsDaemon({
66-
// Directory to which save IPFS data to
67-
IpfsDataDir: dirInput.value,
68-
// IPFS dev server: webrtc-star-signalling.cloud.ipfs.team
69-
SignalServer: signalServerInput.value,
70-
})
71-
7276
// Update the UI with initial settings
7377
updateView('starting', ipfs)
7478

75-
// Wait for IPFS to start
76-
ipfs.on('ready', () => {
77-
// Update the UI
78-
updateView('ready', ipfs)
79+
// Create an IPFS instance
80+
// window.startIpfs() is exposed in ./start-ipfs.js
81+
window.startIpfs(ipfsOptions, (err, node) => {
82+
if (err) {
83+
onError(err)
84+
return
85+
}
7986

80-
// Poll for peers from IPFS and display them
81-
pollPeersTimer = setInterval(updatePeers, 1000)
82-
peers.innerHTML = '<h2>Peers</h2><i>Waiting for peers...</i>'
83-
})
87+
ipfs = node
88+
89+
// Get our IPFS instance's info: ID and address
90+
ipfs.id().then((id) => {
91+
peerInfo = id
92+
// Update the UI
93+
updateView('ready', ipfs)
8494

85-
// Handle IPFS errors
86-
ipfs.on('error', onError)
95+
// Poll for peers from IPFS and display them
96+
pollPeersTimer = setInterval(updatePeers, 1000)
97+
peers.innerHTML = '<h2>Peers</h2><i>Waiting for peers...</i>'
98+
})
99+
})
87100
}
88101
}
89102

@@ -93,7 +106,7 @@ <h2>Files</h2>
93106
if (pollPeersTimer)
94107
clearInterval(pollPeersTimer)
95108

96-
ipfs.stop()
109+
ipfs.goOffline()
97110
ipfs = null
98111
updateView('stopped', ipfs)
99112
}
@@ -241,9 +254,9 @@ <h2>Files</h2>
241254
details.innerHTML = '<div>'
242255
+ '<h2>IPFS Node</h2>'
243256
+ '<b>ID</b><br>'
244-
+ ipfs.PeerId + '<br><br>'
257+
+ peerInfo.id + '<br><br>'
245258
+ '<b>Address</b><br>'
246-
+ ipfs.APIAddress[0] + '<br><br>'
259+
+ peerInfo.addresses[0] + '<br><br>'
247260
+ '<b>IPFS Data Directory</b><br>'
248261
+ dirInput.value
249262
// Set the file status
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
// Start an IPFS instance
4+
window.startIpfs = (options, callback) => {
5+
const repoPath = options.IpfsDataDir || '/tmp/ipfs'
6+
7+
const node = new window.Ipfs(repoPath)
8+
9+
node.init({ emptyRepo: true, bits: 2048 }, (err) => {
10+
if (err && err.message !== 'repo already exists') {
11+
return callback(err)
12+
}
13+
14+
node.config.get((err, config) => {
15+
if (err) {
16+
return callback(err)
17+
}
18+
19+
const host = options.SignalServer.split(':')[0] || '127.0.0.1'
20+
const port = options.SignalServer.split(':')[1] || 9090
21+
const signalServer = `/libp2p-webrtc-star/ip4/${host}/tcp/${port}/ws/ipfs/${config.Identity.PeerID}`
22+
23+
config.Addresses = {
24+
Swarm: [
25+
signalServer
26+
],
27+
API: '',
28+
Gateway: ''
29+
}
30+
31+
config.Discovery.MDNS.Enabled = false
32+
33+
node.config.replace(config, (err) => {
34+
if (err) { return callback(err) }
35+
36+
node.load((err) => {
37+
if (err) { return callback(err) }
38+
node.goOnline((err) => {
39+
if (err) { return callback(err) }
40+
console.log('IPFS node is ready')
41+
callback(null, node)
42+
})
43+
})
44+
})
45+
})
46+
})
47+
}

examples/interop-examples/README.md

Lines changed: 0 additions & 102 deletions
This file was deleted.

examples/interop-examples/package.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)