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

Commit

Permalink
docs: last touches to the README and browser-script-tag example
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Mar 13, 2017
1 parent fea0d52 commit ebb0717
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int
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.
* `ipfs.types.Buffer`
* [`ipfs.types.Buffer`](https://www.npmjs.com/package/buffer)
* [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id)
* [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info)
* [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr)
Expand Down
2 changes: 0 additions & 2 deletions examples/browser-script-tag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ You can use IPFS in your in-browser JavaScript code with just a `<script>` tag.
This exposes a global `Ipfs`; you can get a node by making a `new Ipfs()`.

See `index.html` for a working example.


63 changes: 29 additions & 34 deletions examples/browser-script-tag/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,55 @@
<title>IPFS in the Browser</title>
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
<script type="text/javascript">
// Set this if you have a libp2p-webrtc-star server
// Something like
// var SIGNALING_SERVER = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/'
// We provide a hosted signalling endpoint that you can use to discover
// and dial to other nodes. It is hosted at `star-signal.cloud.ipfs.team`
// If you run your own signalling, you can change this multiaddr.
const SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/'

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

// Create an IPFS node
var node = new Ipfs()
const node = new Ipfs({
repo: repoPath
})

// Init the node
node.init(handleInit)

function handleInit (err) {
// The node exited with an error
if (err && err.message !== 'repo already exists') {
if (!err) { // The repo was initialized for the first time, we need to configure it
addWebRTCMultiaddr()
} else if (err && err.message !== 'repo already exists') { // The repo already existed, let's just load it
loadRepo()
} else {
throw err
}

// The repo was initialized for the first time, we need to configure it
if (!err) configNode()
// The repo already existed, let's just load it
loadRepo()
}

function configNode() {
function addWebRTCMultiaddr() {
// Addj the WebrTCStar Multiaddr to your node
node.config.get(function (err, config) {
if (err) {
throw err
}
// Add at least one libp2p-webrtc-star address.
var star_addr = (SIGNALING_SERVER + config.Identity.PeerID)
node.config.set('Addresses.Swarm[1]', star_addr, function (err) {
if (err) {
throw err
}
// Load the newly created repo
loadRepo()
})

const starAddr = (SIGNALING_SERVER + config.Identity.PeerID)

node.config.set('Addresses.Swarm[1]', starAddr, loadRepo)
})
}

function loadRepo() {
node.load(() => {
// Go online and connect to things
node.goOnline(() => {
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')
document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')
// TODO: Write your code here!
// Use methods like node.files.add, node.files.get, and so on
// Methods requiring buffers can use node.types.Buffer
})
})
node.load(() => node.goOnline(() => {
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')

document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')

// \o/ Now you have an IPFS node using WebRTC to find other nodes!
// You can write more code here to use it. Use methods like
// node.files.add, node.files.get. See the API docs here:
// https://github.com/ipfs/interface-ipfs-core/tree/master/API
}))
}
</script>
</head>
Expand Down

0 comments on commit ebb0717

Please sign in to comment.