You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+42-15
Original file line number
Diff line number
Diff line change
@@ -39,9 +39,11 @@ Consult the [Roadmap](/ROADMAP.md) for a complete state description of the proje
39
39
-[HTTP-API](#http-api)
40
40
-[IPFS Core examples (use IPFS as a module)](#ipfs-core-examples-use-ipfs-as-a-module)
41
41
-[Create a IPFS node instance](#create-a-ipfs-node-instance)
42
-
-[Startup (in Node.js or browser)](#startup-in-node.js-or-browser)
43
-
-[Adding a file](#adding-a-file)
44
-
-[Retrieving a file](#retrieving-a-file)
42
+
-[Find IPFS in Node.js](#find-ipfs-in-nodejs)
43
+
-[Find IPFS in the Browser](#find-ipfs-in-the-browser)
44
+
-[Robust Initialization and libp2p-webrtc-star Signaling](#robust-initialization-and-libp2p-webrtc-star-signaling)
45
+
-[Add a file](#add-a-file)
46
+
-[Retrieve a file](#retrieve-a-file)
45
47
-[More to come](#more-to-come)
46
48
-[API](#api)
47
49
-[Generic API](#generic-api)
@@ -146,6 +148,8 @@ The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https:/
146
148
147
149
#### Create a IPFS node instance
148
150
151
+
The basic startup flow involves (optionally) creating a Repo, creating an IPFS node, `init`-ing it so it can generate its keys, `load`-ing its configuration, and putting it online with `goOnline`. Here is a structural example:
152
+
149
153
```JavaScript
150
154
// IPFS will need a repo, it can create one for you or you can pass
Below are some more examples of JavaScript IPFS in action.
181
185
182
-
#### Startup (in Node.js or browser)
186
+
#### Find IPFS in Node.js
183
187
184
-
There's still a bit of work required to start up a node in a robust way (i.e. without requiring the user to manually `js-ipfs init`). Here is one way to do it.
188
+
If you are working in Node.js and did `npm install ipfs`, or if you are working in a browser environment that exposes Node builtins like `require`, you should get an IPFS node like this:
185
189
186
190
```javascript
187
-
// Find the IPFS implementation
188
-
constIPFS=typeof Ipfs !=='undefined'? Ipfs // browser global
If you are in a normal browser Javascript environment, and loaded IPFS via the minified build or the [unpkg CDN](#use-in-a-browser-using-a-script-tag), you should get an IPFS node like this:
198
+
199
+
```javascript
200
+
var ipfs =newIpfs()
201
+
```
202
+
203
+
Make sure your script loads after the IPFS library, so that the `Ipfs` global will be defined.
204
+
205
+
#### Robust Initialization and libp2p-webrtc-star Signaling
206
+
207
+
There's still a bit of work required to start up an in-browser node in a robust way, so that it will work whether or not there is an existing initialized IPFS repo in the user's browser. If there isn't one, you need to call `init` as above, but if there is one, calling `init` will fail. Moreover, there's currently no good way to check if you need to call `init` or not.
208
+
209
+
Also, an in-browser node isn't able to call up normal IPFS nodes over raw TCP; it can only communicate over Websockets and WebRTC. Currently, there are no Websockets or WebRTC bootstrap nodes run by the IPFS maintainers. You will probably want to set up a [libp2p-webrtc-star signaling server](https://github.com./libp2p/js-libp2p-webrtc-star) so nodes used in your application can find each other:
210
+
211
+
```bash
212
+
npm i libp2p-webrtc-star -g
213
+
star-sig
214
+
```
215
+
216
+
You will then want to point IPFS nodes used in your application at your signaling server, so they can connect to each other. This is accomplished by adding an address to the node's configuration referencing the signaling server, of the form `/libp2p-webrtc-star/ip4/<server-ip>/tcp/<server-port>/ws/ipfs/<peer-id>`, where `<peer-id>` is the peer ID of the node that the address is being added to. This causes the node to think of itself as being contactable through the signaling server. It will then initializes its libp2p-webrtc-star implementation and automatically peer with other nodes using the same server.
217
+
218
+
Below is an example which initializes an IPFS node in a browser safely, whether a node has already been initialized by the current domain or not. It also configures `libp2p-webrtc-star` communication, using a signaling server running on the local host. (Note that since IPFS node configuration information is stored in IndexedDB in browsers, opening two tabs of this code from a local file in the same browser won't work, because they'll share the same node keys and identity. Either run the code from multiple domains, or run it in two different browsers, like Chrome and Firefox.)
219
+
220
+
```javascript
221
+
// We assume you already have your IPFS node in `ipfs`, as in the two examples above.
194
222
195
223
// Init a repo in the given IPFS node it if hasn't got one already. Calls the
196
224
// setup callback, passing the normal callback, after first initialization.
Once you have an IPFS node up and running, you can add files to it from `Buffer`s, `Readable` streams, or [arrays of objects of a certain form](https://github.com./ipfs/interface-ipfs-core/tree/master/API/files#add). If you don't have `Buffer` conveniently available (say, because you're in a browser without the Node API handy), it's available as a property of the IPFS node.
To retrieve the contents of a file, you can use the [cat method](https://github.com./ipfs/interface-ipfs-core/tree/master/API/files#cat), which will call your callback with a Node.js-style `Readable` stream.
// Use methods like ipfs.files.add, ipfs.files.get, and so on in here
75
+
// Methods requiring buffers can use ipfs.Buffer
76
+
})
77
+
})
78
+
})
79
+
</script>
80
+
</head>
81
+
<body>
82
+
<h1>IPFS in the Browser</h1>
83
+
<p>This page creates an IPFS node in your browser and drops it into the global Javascript namespace as <em>ipfs</em>. Open the console to play around with it.</p>
84
+
<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>
0 commit comments