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

Commit

Permalink
feat: websocket-star? what's that. only heard of stardust!
Browse files Browse the repository at this point in the history
(...psst, we're replacing ws-star with stardust, which does
the same things, except it's way faster & more secure)
  • Loading branch information
mkg20001 committed Oct 31, 2019
1 parent a61d510 commit 9969981
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 35 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1003,22 +1003,24 @@ The code above assumes you are running a local `signaling server` on port `9090`
#### Is there a more stable alternative to webrtc-star that offers a similar functionality?
Yes, websocket-star! A WebSockets based transport that uses a Relay to route the messages. To enable it, just do:
Yes, stardust! A WebSockets based transport that uses a Relay to route the messages. To enable it, just do:
```JavaScript
const node = await IPFS.create({
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
'/dns4/stardust.mkg20001.io/tcp/443/wss/p2p-stardust'
]
}
}
})

// your instance with websocket-star is ready
// your instance with stardust is ready
```
(You may need to install the stardust module as well, with `npm install libp2p-stardust`)
#### I see some slowness when hopping between tabs Chrome with IPFS nodes, is there a reason why?
Yes, unfortunately, due to [Chrome aggressive resource throttling policy](https://github.com/ipfs/js-ipfs/issues/611), it cuts freezes the execution of any background tab, turning an IPFS node that was running on that webpage into a vegetable state.
Expand Down
2 changes: 1 addition & 1 deletion examples/circuit-relaying/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ In order to enable the relay functionality in `go-ipfs` we need to edit it's con

The two options we're looking for are `DisableRelay` and `EnableRelayHop`. We want the former (`DisableRelay`) set to `false` and the latter (`EnableRelayHop`) to `true`, just like in the example above. That should set our go node as a relay.

We also need to make sure our go node can be dialed from the browser. For that, we need to enable a transport that both the browser and the go node can communicate over. We will use the web sockets transport, although there are others that can be used, such as `webrtc-star` and `websocket-star`. To enable the transport and set the interface and port we need to edit the `~/.ipfs/config` one more time. Let's find the `Swarm` array and add our desired address there. I picked `/ip4/0.0.0.0/tcp/4004/ws` because it is a port I know is not being used by anything on my machine, but we can also use port `0` so that the OS chooses a random available port for us — either one should work.
We also need to make sure our go node can be dialed from the browser. For that, we need to enable a transport that both the browser and the go node can communicate over. We will use the web sockets transport, although there are others that can be used, such as `webrtc-star` and `stardust`. To enable the transport and set the interface and port we need to edit the `~/.ipfs/config` one more time. Let's find the `Swarm` array and add our desired address there. I picked `/ip4/0.0.0.0/tcp/4004/ws` because it is a port I know is not being used by anything on my machine, but we can also use port `0` so that the OS chooses a random available port for us — either one should work.

```json
"Swarm": [
Expand Down
10 changes: 5 additions & 5 deletions examples/custom-libp2p/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Libp2p = require('libp2p')
const IPFS = require('ipfs')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WebSocketStar = require('libp2p-websocket-star')
const Stardust = require('libp2p-stardust')
const Bootstrap = require('libp2p-bootstrap')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
Expand Down Expand Up @@ -32,8 +32,8 @@ const libp2pBundle = (opts) => {
const peerBook = opts.peerBook
const bootstrapList = opts.config.Bootstrap

// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
const wsstar = new WebSocketStar({
// Create our Stardust transport and give it our PeerId, straight from the ipfs node
const wsstar = new Stardust({
id: peerInfo.id
})

Expand All @@ -50,7 +50,7 @@ const libp2pBundle = (opts) => {
modules: {
transport: [
TCP,
wsstar
stardust
],
streamMuxer: [
MPLEX,
Expand All @@ -62,7 +62,7 @@ const libp2pBundle = (opts) => {
peerDiscovery: [
MulticastDNS,
Bootstrap,
wsstar.discovery
stardust.discovery
],
dht: KadDHT
},
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.13.3",
"libp2p-tcp": "~0.13.0",
"libp2p-websocket-star": "~0.10.2",
"libp2p-stardust": "~0.1.2",
"pull-mplex": "~0.1.0"
}
}
2 changes: 1 addition & 1 deletion examples/exchange-files-in-browser/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function start () {
repo: 'ipfs-' + Math.random(),
config: {
Addresses: {
Swarm: ['/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star']
Swarm: ['/dns4/stardust.mkg20001.io/tcp/443/wss/p2p-stardust']
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
["libp2p/js-libp2p-bootstrap", "libp2p-bootstrap"],
["libp2p/js-libp2p-secio", "libp2p-secio"],
["libp2p/js-libp2p-tcp", "libp2p-tcp"],
["libp2p/js-libp2p-stardust", "libp2p-stardust"],
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
["libp2p/js-libp2p-websockets", "libp2p-websockets"],
["libp2p/pull-mplex", "pull-mplex"],

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
"libp2p-secio": "~0.11.0",
"libp2p-tcp": "^0.13.0",
"libp2p-webrtc-star": "~0.16.0",
"libp2p-websocket-star-multi": "~0.4.3",
"libp2p-websockets": "~0.12.3",
"lodash": "^4.17.15",
"lodash.flatten": "^4.4.0",
Expand Down Expand Up @@ -207,7 +206,7 @@
"interface-ipfs-core": "^0.117.2",
"ipfs-interop": "^0.1.1",
"ipfsd-ctl": "^0.47.2",
"libp2p-websocket-star": "~0.10.2",
"libp2p-stardust": "~0.1.2",
"ncp": "^2.0.0",
"p-event": "^4.1.0",
"qs": "^6.5.2",
Expand Down
8 changes: 0 additions & 8 deletions src/core/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const WS = require('libp2p-websockets')
const WebRTCStar = require('libp2p-webrtc-star')
const WebSocketStarMulti = require('libp2p-websocket-star-multi')
const Multiplex = require('pull-mplex')
const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')
Expand All @@ -16,12 +15,6 @@ class Node extends libp2p {
constructor (_options) {
const wrtcstar = new WebRTCStar({ id: _options.peerInfo.id })

// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
const wsstarServers = _options.peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star'))
_options.peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: _options.peerInfo.id, ignore_no_online: !wsstarServers.length || _options.wsStarIgnoreErrors })

const defaults = {
switch: {
denyTTL: 2 * 60 * 1e3, // 2 minute base
Expand All @@ -44,7 +37,6 @@ class Node extends libp2p {
],
peerDiscovery: [
wrtcstar.discovery,
wsstar.discovery,
Bootstrap
],
dht: KadDHT,
Expand Down
10 changes: 1 addition & 9 deletions src/core/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const WebSocketStarMulti = require('libp2p-websocket-star-multi')
const Bootstrap = require('libp2p-bootstrap')
const KadDHT = require('libp2p-kad-dht')
const GossipSub = require('libp2p-gossipsub')
Expand All @@ -15,12 +14,6 @@ const multiaddr = require('multiaddr')

class Node extends libp2p {
constructor (_options) {
// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
const wsstarServers = _options.peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star'))
_options.peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: _options.peerInfo.id, ignore_no_online: !wsstarServers.length || _options.wsStarIgnoreErrors })

const defaults = {
switch: {
denyTTL: 2 * 60 * 1e3, // 2 minute base
Expand All @@ -43,8 +36,7 @@ class Node extends libp2p {
],
peerDiscovery: [
MulticastDNS,
Bootstrap,
wsstar.discovery
Bootstrap
],
dht: KadDHT,
pubsub: GossipSub
Expand Down
8 changes: 4 additions & 4 deletions test/core/libp2p.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha')
const MemoryStore = require('interface-datastore').MemoryDatastore
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const WebSocketStar = require('libp2p-websocket-star')
const Stardust = require('libp2p-stardust')
const Multiplex = require('pull-mplex')
const SECIO = require('libp2p-secio')
const KadDHT = require('libp2p-kad-dht')
Expand Down Expand Up @@ -71,14 +71,14 @@ describe('libp2p customization', function () {
_print: console.log,
_options: {
libp2p: (opts) => {
const wsstar = new WebSocketStar({ id: opts.peerInfo.id })
const stardust = new Stardust({ id: opts.peerInfo.id })

return new Libp2p({
peerInfo: opts.peerInfo,
peerBook: opts.peerBook,
modules: {
transport: [
wsstar
stardust
],
streamMuxer: [
Multiplex
Expand All @@ -87,7 +87,7 @@ describe('libp2p customization', function () {
SECIO
],
peerDiscovery: [
wsstar.discovery
stardust.discovery
],
dht: KadDHT
}
Expand Down

0 comments on commit 9969981

Please sign in to comment.