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

Commit

Permalink
simplify usage of WebRTC with js-ipfs in Node.js (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias authored Jul 23, 2017
1 parent b7e8e88 commit 79ea7ac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
69 changes: 46 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,6 @@ Commands:
- default API port: `5002`
- default Bootstrap is off, to enable it set `IPFS_BOOTSTRAP=1`

If you want to use WebRTC in your local daemon, you will need to install it separatly by installing globally one of the following modules:

- [wrtc](https://npmjs.org/wrtc)
- [electron-webrtc](https://npmjs.org/electron-webrtc)

This is a separate step because there isn't still a good open source WebRTC implementation for Node.js that runs in all the envinronments correctly.
Example: `npm install electron-webrtc --global`.

### HTTP-API

Expand Down Expand Up @@ -193,11 +186,6 @@ When starting a node, you can:
// https://github.com/ipfs/js-ipfs-repo
const repo = <IPFS Repo instance or repo path>

// If you want to use the WebRTC transport in Node.js, you have to add it separately. Note, WebRTC comes out of the box in the Browser!
const wrtc = require('wrtc') // or require('electron-webrtc')()
const WStar = require('libp2p-webrtc-star')
const wstar = WStar({ wrtc: wrtc })

const node = new IPFS({
repo: repo,
init: true, // default
Expand All @@ -210,21 +198,17 @@ const node = new IPFS({
EXPERIMENTAL: { // enable experimental features
pubsub: true,
sharding: true, // enable dir sharding
wrtcLinuxWindows: true, // use unstable wrtc module on Linux or Windows with Node.js,
dht: true // enable KadDHT, currently not interopable with go-ipfs
},
config: { // overload the default config
config: { // overload the default IPFS node config
Addresses: {
Swarm: [
'/ip4/127.0.0.1/tcp/1337'
]
}
},
libp2p: { // add custom modules to the libp2p stack of your node
modules: { // here we show how to add WebRTC. Note, WebRTC comes out of the box in the Browser! You just need to do this for Node.js
transport: [wstar]
discovery: [wstar.discovery]
}
modules: {}
}
})

Expand Down Expand Up @@ -356,15 +340,54 @@ A set of data types are exposed directly from the IPFS instance under `ipfs.type

## FAQ

> Is there WebRTC support for js-ipfs with Node.js?
#### Is there WebRTC support for js-ipfs with Node.js?

Yes there is, however, Linux and Windows support is limited/unstable. For Linux users, you need to follow the install the extra packages for Linux listed on the [`wrtc` npm page](http://npmjs.org/wrtc) and then, when doing initing the repo, do:
Yes, however, bare in mind that there isn't a 100% stable solution to use WebRTC in Node.js, use it at your own risk. The most tested options are:

```sh
> IPFS_WRTC_LINUX_WINDOWS=1 jsipfs init
- [wrtc](https://npmjs.org/wrtc) - Follow the install instructions.
- [electron-webrtc](https://npmjs.org/electron-webrtc)

To add WebRTC support in a IPFS node instance, do:

```JavaScript
const wrtc = require('wrtc') // or require('electron-webrtc')()
const WStar = require('libp2p-webrtc-star')
const wstar = WStar({ wrtc: wrtc })

const node = new IPFS({
repo: 'your-repo-path',
// start: false,
config: {
Addresses: {
Swarm: [
"/ip4/0.0.0.0/tcp/4002",
"/ip4/127.0.0.1/tcp/4003/ws",
"/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss"
]
}
},
libp2p: {
modules: {
transport: [wstar]
discovery: [wstar.discovery]
}
}
})

node.on('ready', () => {
// your instance with WebRTC is ready
})
```

To add WebRTC support to the IPFS daemon, you only need to install one of the WebRTC modules globally:

```bash
npm install wrtc --global
# or
npm install electron-webrtc --global
```

This will create a repo with a config file that contains a WebRTC multiaddr.
Then, update your IPFS Daemon config to include the multiaddr for this new transport on the `Addresses.Swarm` array. Add: `"/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss"`

## Packages

Expand Down
14 changes: 0 additions & 14 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@ module.exports = function init (self) {
opts.log('done')
opts.log('peer identity: ' + config.Identity.PeerID)

const isWin = /^win/.test(process.platform)
const isLinux = /^linux/.test(process.platform)
const wrtcLinuxWindows = !process.env.IPFS_WRTC_LINUX_WINDOWS ||
self._options.EXPERIMENTAL.wrtcLinuxWindows

// For the lack of sane WebRTC support on Linux and Windows
if (wrtcLinuxWindows && (isWin || isLinux)) {
console.log('WARNING: Your platform does not have native WebRTC support, it won\' use any WebRTC transport')
const newAddrs = config.Addresses.Swarm.filter((addr) => {
return addr.indexOf('libp2p-webrtc-star') < 0
})
config.Addresses.Swarm = newAddrs
}

self._repo.init(config, cb)
},
(_, cb) => self._repo.open(cb),
Expand Down
3 changes: 1 addition & 2 deletions src/core/runtime/config-nodejs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"Addresses": {
"Swarm": [
"/ip4/0.0.0.0/tcp/4002",
"/ip4/127.0.0.1/tcp/4003/ws",
"/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss"
"/ip4/127.0.0.1/tcp/4003/ws"
],
"API": "/ip4/127.0.0.1/tcp/5002",
"Gateway": "/ip4/127.0.0.1/tcp/9090"
Expand Down

0 comments on commit 79ea7ac

Please sign in to comment.