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

Commit 2156ea5

Browse files
committed
docs: last touches to the README and browser-script-tag example
1 parent fea0d52 commit 2156ea5

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int
261261
262262
A set of data types are exposed directly from the IPFS instance under `ipfs.types`. That way you're not required to import/require the following.
263263
264-
* `ipfs.types.Buffer`
264+
* [`ipfs.types.Buffer`](https://www.npmjs.com/package/Buffer)
265265
* [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id)
266266
* [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info)
267267
* [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr)

examples/browser-script-tag/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ You can use IPFS in your in-browser JavaScript code with just a `<script>` tag.
99
This exposes a global `Ipfs`; you can get a node by making a `new Ipfs()`.
1010

1111
See `index.html` for a working example.
12-
13-

examples/browser-script-tag/index.html

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,55 @@
44
<title>IPFS in the Browser</title>
55
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
66
<script type="text/javascript">
7-
// Set this if you have a libp2p-webrtc-star server
8-
// Something like
9-
// var SIGNALING_SERVER = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/'
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/'
1011

11-
// This is our signalling server that can be used for practical demos and experimentation.
12-
// It should not be used for apps in production.
13-
var SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/'
12+
const repoPath = 'ipfs-' + Math.random()
1413

1514
// Create an IPFS node
16-
var node = new Ipfs()
15+
const node = new Ipfs({
16+
repo: repoPath
17+
})
1718

1819
// Init the node
1920
node.init(handleInit)
2021

2122
function handleInit (err) {
22-
// The node exited with an error
23-
if (err && err.message !== 'repo already exists') {
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 {
2428
throw err
2529
}
26-
27-
// The repo was initialized for the first time, we need to configure it
28-
if (!err) configNode()
29-
// The repo already existed, let's just load it
30-
loadRepo()
3130
}
3231

33-
function configNode() {
32+
function addWebRTCMultiaddr() {
33+
// Addj the WebrTCStar Multiaddr to your node
3434
node.config.get(function (err, config) {
3535
if (err) {
3636
throw err
3737
}
38-
// Add at least one libp2p-webrtc-star address.
39-
var star_addr = (SIGNALING_SERVER + config.Identity.PeerID)
40-
node.config.set('Addresses.Swarm[1]', star_addr, function (err) {
41-
if (err) {
42-
throw err
43-
}
44-
// Load the newly created repo
45-
loadRepo()
46-
})
38+
39+
const starAddr = (SIGNALING_SERVER + config.Identity.PeerID)
40+
41+
node.config.set('Addresses.Swarm[1]', starAddr, loadRepo)
4742
})
4843
}
4944

5045
function loadRepo() {
51-
node.load(() => {
52-
// Go online and connect to things
53-
node.goOnline(() => {
54-
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')
55-
document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')
56-
// TODO: Write your code here!
57-
// Use methods like node.files.add, node.files.get, and so on
58-
// Methods requiring buffers can use node.types.Buffer
59-
})
60-
})
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+
}))
6156
}
6257
</script>
6358
</head>

0 commit comments

Comments
 (0)