libp2p / js-libp2p-webrtc-star

libp2p WebRTC transport that includes a discovery mechanism provided by the signalling-star
https://libp2p.io
Other
320 stars 96 forks source link

Not getting peers nor pubsub messages #268

Closed markg85 closed 3 years ago

markg85 commented 3 years ago

Hi,

I have setup a "webrtc-star" server here: https://star.sc2.nl/ Next i use the following code that is compiled to browser code using parcel. You can (nearly literally) copy/paste it in your own setup.

import 'babel-polyfill';
import Libp2p from 'libp2p'
// const TCP = require('libp2p-tcp')
import Websockets from 'libp2p-websockets'
import WebRTCStar from 'libp2p-webrtc-star'
import { NOISE } from 'libp2p-noise'
import Mplex from 'libp2p-mplex'
import Bootstrap from 'libp2p-bootstrap'
import GossipSub from 'libp2p-gossipsub'
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')

const bootstrapMultiaddrs = [
    '/dnsaddr/ipfs.sc2.nl/tcp/4001/p2p/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N',
    // '/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
    // '/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
    // '/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
    // '/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
    // '/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
    // '/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
    // '/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
    // '/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
    // '/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
]

const createNode = async () => {
    const node = await Libp2p.create({
        addresses: {
            // Add the signaling server address, along with our PeerId to our multiaddrs list
            // libp2p will automatically attempt to dial to the signaling server so that it can
            // receive inbound connections from other peers
            listen: [
                '/dnsaddr/star.sc2.nl/tcp/443/wss/p2p-webrtc-star/p2p/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N',
                // '/dnsaddr/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
                // '/dnsaddr/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
            ]
        },
        modules: {
            // transport: [TCP],
            transport: [Websockets, WebRTCStar],
            streamMuxer: [Mplex],
            connEncryption: [NOISE],
            pubsub: GossipSub,
            peerDiscovery: [Bootstrap]
        },
        config: {
            peerDiscovery: {
                autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
                // The `tag` property will be searched when creating the instance of your Peer Discovery service.
                // The associated object, will be passed to the service when it is instantiated.
                [Bootstrap.tag]: {
                    enabled: true,
                    list: bootstrapMultiaddrs // provide array of multiaddrs
                }
            },
            pubsub: {
                enabled: true,
                emitSelf: false
            }
        }
    })

    await node.start()
    return node
}

(async () => {
    const topic = 'news'

    const node = await createNode();

    const listenAddrs = node.transportManager.getAddrs()
    console.log('libp2p is listening on the following addresses: ', listenAddrs)

    const advertiseAddrs = node.multiaddrs
    console.log('libp2p is advertising the following addresses: ', advertiseAddrs)

    node.on('peer:discovery', (peer) => {
        console.log('Discovered %s', peer.toB58String()) // Log discovered peer
    })

    node.connectionManager.on('peer:connect', (connection) => {
        console.log('Connected to %s', connection.remotePeer.toB58String()) // Log connected peer
    })

    node.pubsub.on(topic, (msg) => {
        console.log(`node received: ${uint8ArrayToString(msg.data)}`)
    })
    await node.pubsub.subscribe(topic)

    setInterval(() => {
        node.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
    }, 1000)
})()

Note, please do tell me anything that's wrong with this code. I've merely scraped it together from multiple examples throughout readme pages, examples and issues posts.. It seems very difficult to get this working properly.

In the code above, i'm connecting (at least i think listen does that? shouldn't bootstrap connect? ... confusion all the way) to: /dnsaddr/star.sc2.nl/tcp/443/wss/p2p-webrtc-star/p2p/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N

On the console i do get two outputs with that where it claims to be listening on that address and advertising. But i'm not receiving any node connections, not after waiting for hours...

Next, if i try /dnsaddr/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star (star 2) as listen address i do get nodes quite quickly. But it's advised to not use that. Also, if i post any message on the "news" pubsub room with an IPFS client, i'm not getting any message here.

Lastly, the /dnsaddr/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star (star1) is just there because this library gives me these two as defaults. Now note though that only star2 seems to be giving any peers.

I'm very likely just doing something wrong here. What, i have no clue about. At all. So err, help please :)

Cheers, Mark

vasco-santos commented 3 years ago

Hey Mark, Thanks for reaching out. For properly testing this out in my end, can you let me know your versions of every module in the package.json? If you have this available in any repo, it would be nice.

Just looking at the code, I can see some issues. Starting by the bootstrap multiaddrs, I believe you are adding the address of the signal star server. It is not a libp2p node, so it is not reachable. Moreover, we do not support dialling dnsaddr at the moment (should land soon: https://github.com/libp2p/js-libp2p/pull/782). In the listen addresses, which will be used in this case by the browser to bind to a signal server. This needs to be used because browsers do not support listening for new connections. This way, the browser node needs someone else to listen for connections on its behalf. You should not add the peer id in the listen address: /dnsaddr/star.sc2.nl/tcp/443/wss/p2p-webrtc-star

Just using your own signal server, you should be able to open multiple browsers in the same machine, or even in different machines and they will discover and connect to each other.

markg85 commented 3 years ago

Hi Vasco,

Do note that the address i was using in the bootstrap was ipfs.sc2.nl note the ipfs. That is a full IPFS node. The webrtc thingy is running at star.sc2.nl there note the star :)

I fiddled with the addresses a bit to reflect what you just told me. It didn't change anything for me (no peers). So yeah, i'll happily share the full test project!

Also, you mentioned that i should not use the p2p-webrtc-star with peer id. I merely got that from https://github.com/libp2p/js-libp2p-webrtc-star which mentions:

A libp2p-webrtc-star address, using the signaling server we provide, looks like:

/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star/p2p/<your-peer-id>

which made me think that i need to add it... I immediately believe you tell me otherwise as you're the developer of that project :) It just goes to show that instructions on getting this working are.... ehm... super vague at best. I'll try to help a bit there when we get this working.

package.json Be aware that i was using webpack and just changed to parcel. The dependencies in the package file might therefore be still a bit too much. It all should be fully up to date as i made this just a couple of days ago.

{
  "name": "testproject",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "build": "parcel build src/index.html",
    "start": "parcel src/index.html"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-polyfill": "^6.26.0",
    "libp2p": "^0.29.2",
    "libp2p-bootstrap": "^0.12.1",
    "libp2p-gossipsub": "^0.6.4",
    "libp2p-mplex": "^0.10.1",
    "libp2p-noise": "^2.0.1",
    "libp2p-pubsub-peer-discovery": "^3.0.0",
    "libp2p-tcp": "^0.15.1",
    "libp2p-webrtc-star": "^0.20.1",
    "libp2p-websockets": "^0.14.0",
    "os-browserify": "^0.3.0",
    "parcel-bundler": "^1.12.4",
    "uint8arrays": "^1.1.0"
  },
  "devDependencies": {}
}

index.js

import 'babel-polyfill';
import Libp2p from 'libp2p'
// const TCP = require('libp2p-tcp')
import Websockets from 'libp2p-websockets'
import WebRTCStar from 'libp2p-webrtc-star'
import { NOISE } from 'libp2p-noise'
import Mplex from 'libp2p-mplex'
import Bootstrap from 'libp2p-bootstrap'
import GossipSub from 'libp2p-gossipsub'
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')

const bootstrapMultiaddrs = [
    '/ip4/116.203.242.65/tcp/4001/p2p/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N',
    // '/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
    // '/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
    // '/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
    // '/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
    // '/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
    // '/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
    // '/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
    // '/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
    // '/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
]

const createNode = async () => {
    const node = await Libp2p.create({
        addresses: {
            // Add the signaling server address, along with our PeerId to our multiaddrs list
            // libp2p will automatically attempt to dial to the signaling server so that it can
            // receive inbound connections from other peers
            listen: [
                // '/ip4/0.0.0.0/tcp/0'
                '/dns4/star.sc2.nl/tcp/443/wss/p2p-webrtc-star',
                // '/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
                // '/dnsaddr/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
            ]
        },
        modules: {
            // transport: [TCP],
            transport: [Websockets, WebRTCStar],
            streamMuxer: [Mplex],
            connEncryption: [NOISE],
            pubsub: GossipSub,
            peerDiscovery: [Bootstrap]
        },
        config: {
            peerDiscovery: {
                autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
                // The `tag` property will be searched when creating the instance of your Peer Discovery service.
                // The associated object, will be passed to the service when it is instantiated.
                [Bootstrap.tag]: {
                    enabled: true,
                    list: bootstrapMultiaddrs // provide array of multiaddrs
                }
            },
            pubsub: {
                enabled: true,
                emitSelf: false
            }
        }
    })

    await node.start()
    return node
}

(async () => {
    const topic = 'news'

    const node = await createNode();

    const listenAddrs = node.transportManager.getAddrs()
    console.log('libp2p is listening on the following addresses: ', listenAddrs)

    const advertiseAddrs = node.multiaddrs
    console.log('libp2p is advertising the following addresses: ', advertiseAddrs)

    node.on('peer:discovery', (peer) => {
        console.log('Discovered %s', peer.toB58String()) // Log discovered peer
    })

    node.connectionManager.on('peer:connect', (connection) => {
        console.log('Connected to %s', connection.remotePeer.toB58String()) // Log connected peer
    })

    node.pubsub.on(topic, (msg) => {
        console.log(`node received: ${uint8ArrayToString(msg.data)}`)
    })
    await node.pubsub.subscribe(topic)

    setInterval(() => {
        node.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
    }, 1000)
})()

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>blaaaaaa</title>
    <script src="index.js"></script>
  </head>

  <body>
    <div id="root"></div>
  </body>
</html>

Put index.js and index.html in a folder called src and have package.json in the parent of that src folder. Then run npm install followed by npm run start

vasco-santos commented 3 years ago

Indeed there is an issue with star1. I am trying to gather more information on it. The star2 is working fine.

A libp2p-webrtc-star address, using the signaling server we provide, looks like:

/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star/p2p/<your-peer-id>

The above is true, the peer will be listening on /dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star/p2p/<your-peer-id>. In the first example, the peerId added was not the peerId of the libp2p node being created (it was apparently the IPFS peer id?). We expect listen addresses in configuration to not have a peerId since libp2p will compute the peerId on its creation (unless it is provided).

I will look deeper on your example and get back.

markg85 commented 3 years ago

I did notice that i got peers from star2 too. But even then, pubsub didn't work. In the example i'm subscribing to the "news" topic. Use any ipfs node to do ipfs pubsub pub "news" "blablabla" and you'll see that you won't get the message, however long you wait.

My whole goal here is to have pubsub working, just to be clear about that :) And, that full IPFS node (the one on ipfs.sc2.nl) does have pubsub enabled! It's run with this docker file: https://github.com/markg85/ipfs_extended/blob/main/Dockerfile with the explicit intent to enable pubsub!

vasco-santos commented 3 years ago

Ok, I copied your project and just ran it:

  1. the bootstrap addr is an address using a TCP transport, which is not supported in the browser.

I removed the bootstrap module for now. You will need to use a websocket transport there.

  1. Removing bootstrap, the app started and I immediately got connected to one peer (perhaps it is yours?). I opened a second window and I got a new peer connected and received multiple pubsub messages all around. So, the messages exchange between browser nodes using your signal server seem to be working :) I would recommend logging the peerId in startup node.peerId.toB58String() to understand what peer Id is in each browser windows you have.

--

With this, for achieving your goal you just need to be able to connect with the bootstrap node. I believe the websocket address is not configured by default in go. You need to add that address in the IPFS config file (default in ~/.ipfs/config) with something like:

"Swarm": [
      "/ip4/0.0.0.0/tcp/4001",
      "/ip4/0.0.0.0/tcp/4010/ws",
      "/ip6/::/tcp/4001",
      "/ip6/::/tcp/4010/ws",
      "/ip4/0.0.0.0/udp/4001/quic",
      "/ip6/::/udp/4001/quic"
    ]
vasco-santos commented 3 years ago

@markg85 out of topic, but I remembered that it would be super helpful if you could create docs for setting up the star server, since you got it running. Would you be interested and creating a PR targeting this (in progress PR) https://github.com/libp2p/js-libp2p/pull/718 to add instructions for the star servers?

markg85 commented 3 years ago

Ok, I copied your project and just ran it:

  1. the bootstrap addr is an address using a TCP transport, which is not supported in the browser.

I removed the bootstrap module for now. You will need to use a websocket transport there.

  1. Removing bootstrap, the app started and I immediately got connected to one peer (perhaps it is yours?). I opened a second window and I got a new peer connected and received multiple pubsub messages all around. So, the messages exchange between browser nodes using your signal server seem to be working :) I would recommend logging the peerId in startup node.peerId.toB58String() to understand what peer Id is in each browser windows you have.

--

With this, for achieving your goal you just need to be able to connect with the bootstrap node. I believe the websocket address is not configured by default in go. You need to add that address in the IPFS config file (default in ~/.ipfs/config) with something like:

"Swarm": [
      "/ip4/0.0.0.0/tcp/4001",
      "/ip4/0.0.0.0/tcp/4010/ws",
      "/ip6/::/tcp/4001",
      "/ip6/::/tcp/4010/ws",
      "/ip4/0.0.0.0/udp/4001/quic",
      "/ip6/::/udp/4001/quic"
    ]

I'm lost...

If i understand you correctly you just removed the bootstrapping and that made it work for you? If i do that (this code)

import 'babel-polyfill';
import Libp2p from 'libp2p'
// const TCP = require('libp2p-tcp')
import Websockets from 'libp2p-websockets'
import WebRTCStar from 'libp2p-webrtc-star'
import { NOISE } from 'libp2p-noise'
import Mplex from 'libp2p-mplex'
import GossipSub from 'libp2p-gossipsub'
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')

const createNode = async () => {
    const node = await Libp2p.create({
        addresses: {
            // Add the signaling server address, along with our PeerId to our multiaddrs list
            // libp2p will automatically attempt to dial to the signaling server so that it can
            // receive inbound connections from other peers
            listen: [
                // '/ip4/0.0.0.0/tcp/0'
                '/dns4/star.sc2.nl/tcp/443/wss/p2p-webrtc-star',
                // '/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
                // '/dnsaddr/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
            ]
        },
        modules: {
            // transport: [TCP],
            transport: [Websockets, WebRTCStar],
            streamMuxer: [Mplex],
            connEncryption: [NOISE],
            pubsub: GossipSub,
        },
        config: {
            pubsub: {
                enabled: true,
                emitSelf: false
            }
        }
    })

    await node.start()
    return node
}

(async () => {
    const topic = 'news'

    const node = await createNode();

    const listenAddrs = node.transportManager.getAddrs()
    console.log('libp2p is listening on the following addresses: ', listenAddrs)

    const advertiseAddrs = node.multiaddrs
    console.log('libp2p is advertising the following addresses: ', advertiseAddrs)

    node.on('peer:discovery', (peer) => {
        console.log('Discovered %s', peer.toB58String()) // Log discovered peer
    })

    node.connectionManager.on('peer:connect', (connection) => {
        console.log('Connected to %s', connection.remotePeer.toB58String()) // Log connected peer
    })

    node.pubsub.on(topic, (msg) => {
        console.log(`node received: ${uint8ArrayToString(msg.data)}`)
    })
    await node.pubsub.subscribe(topic)

    setInterval(() => {
        node.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
    }, 1000)
})()

Then i still get the same af before. Nothing.

Also, i don't get why i need to change my IPFS node to talk with websockets. Isn't the whole point of that p2p-webrtc-star to be the proxy of sorts between a website and a ipfs node? Please explain this in detail if you will, i don't get it at all...

markg85 commented 3 years ago

Hmm, i have more results now. If i open 2 tabs with the same app, then it begins to start getting nodes.. wtf...

Ehm... I need to get nodes with just one tab on that app.

Also, am i correct in assuming that if i enable websocket on my IPFS node that i only need the bootstrap node and can get rid of the whole WebRTCStar? Including the signaling thingy on star.sc2.nl?

vasco-santos commented 3 years ago

@markg85

Hmm, i have more results now. If i open 2 tabs with the same app, then it begins to start getting nodes.. wtf...

Yes, that is the expected.

So, on a general note I will explain you the flow for webrtc-star.

Webrtc-star transport+discovery module should be used to easily achieve connectivity between browser peers. Why? Browsers do not support websockets incoming connections, only to dial given addresses (yes, client-server model). What happens behind the scenes? The browser node will dial the star server on start (by adding the webrtc star address as a listen addr). Once a browser node dials the star server, all the other current connections with other browser peers receive a signal that other peer just got connected (Peer discovery). With this, webrtc SDP offers are exchanged in order to create a webrtc connection between browser nodes.

If you simply want a connection between a go-ipfs node and a browser node, the browser node will need to discover the go-ipfs node address (this discovery can be manual, by adding its address to the bootstrap addresses, or even a manual dial). So, a browser node might use Webrtc or Websockets. Currently, go-ipfs webrtc is still being worked on AFAIK, so you should use websockets to achieve this connectivity.

Also, am i correct in assuming that if i enable websocket on my IPFS node that i only need the bootstrap node and can get rid of the whole WebRTCStar? Including the signaling thingy on star.sc2.nl?

Per the above paragraph, yes

Bear in mind that we are currently working on other protocols to improve these flows https://github.com/libp2p/js-libp2p/issues/703

markg85 commented 3 years ago

Ok, right. It slowly begins to make sense.

I couldn't find any of this on the github pages btw.

So i changed my node to allow ws like you mentioned. But now i'm not sure how to modify the code to make use of it. Could you provide an example that uses a ws enabled node in the bootstrap? So no p2p-webrtc-star at all.

You can try to use my node (ipfs.sc2.nl) it should have websockets enabled now.

vasco-santos commented 3 years ago

I couldn't find any of this on the github pages btw.

It is a mixture of concepts. js-libp2p has a wide variety of examples where you would need to get pieces for them. Browser, Transport and Pubsub examples.

But now i'm not sure how to modify the code to make use of it.

For a simple scenario you can do

const node = await Libp2p.create({
        modules: {
            transport: [Websockets],
            streamMuxer: [Mplex],
            connEncryption: [NOISE],
            pubsub: GossipSub,
        },
        config: {
            peerDiscovery: {
                autoDial: true,
                [Bootstrap.tag]: {
                    enabled: true,
                    list: [
                      // Your go ipfs ws address (with peer ID)
                    ]
                }
            },
            pubsub: {
                enabled: true,
                emitSelf: false
            }
        }
    })

Other solution would be to not use bootstrap at all and do:

const node = Libp2p.create({...})
await node.start()
await node.dial(ipfsWebsocketMultiaddr)

You can try to use my node (ipfs.sc2.nl) it should have websockets enabled now.

What port did you use?

markg85 commented 3 years ago

It is a mixture of concepts. js-libp2p has a wide variety of examples where you would need to get pieces for them. Browser, Transport and Pubsub examples.

Please try to improve that. The very fact that it works that way causes it to be very vague for anyone to want to use it. It raises the bar a lot more then it would need to be if there were concise clear examples of explaining individual concepts. Right now it "feels" like on pile of tools where i need to grab some from not knowing fully which tools do what exactly.

What port did you use?

Well, i don't know which port i need to use :) The open ports are those that are advised when making an ipfs docker container. Port 80 and 443 are open too and a let's encrypt certificate is behind it as well. Those those last two are just there due to the nginx reverse proxy living on that host too.

I have: /ip4/116.203.242.65/tcp/4001/p2p/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N

But that's what you said to be wrong for websockets. So i added the websockets part in the config as per your suggestion. Now i don't know which part of that string i need to change to make it work... It can be: /ip4/116.203.242.65/ws/4001/p2p/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N or /ip4/116.203.242.65/tcp/4001/ws/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N or something entirely different... I just don't know.

vasco-santos commented 3 years ago

Please try to improve that. The very fact that it works that way causes it to be very vague for anyone to want to use it. It raises the bar a lot more then it would need to be if there were concise clear examples of explaining individual concepts. Right now it "feels" like on pile of tools where i need to grab some from not knowing fully which tools do what exactly.

It would be great if you could provide direct feedback on what types of examples you would expect and why are they valuable. There are a lot of use cases and at the moment there are a set of examples that enable the basic use cases, but the community input on their difficulties is important.

But that's what you said to be wrong for websockets. So i added the websockets part in the config as per your suggestion.

Yes, but you needed to specify a port: Like I suggested it would be 4010:

"Swarm": [
      "/ip4/0.0.0.0/tcp/4001",
      "/ip4/0.0.0.0/tcp/4010/ws",
      "/ip6/::/tcp/4001",
      "/ip6/::/tcp/4010/ws",
      "/ip4/0.0.0.0/udp/4001/quic",
      "/ip6/::/udp/4001/quic"
    ]

Than the address would be: /ip4/116.203.242.65/tcp/4010/ws/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N

Also, for questions and general support we kindly ask people to use https://discuss.libp2p.io as it will help the entire community, and you can get a broad support. Issues should be for bugs, feature requests, ..., as you can see, we are discussing this issue which is not relevant in webrc-star at all.

markg85 commented 3 years ago

Now i feel stupid as i completely missed the 4010 port part. Oops.

It now slowly begins to work, but still not as i would expect it to. I needed to use this address: /ip4/116.203.242.65/tcp/4010/ws/p2p/12D3KooWLVFqkLQGa4rRFmqrVtTnm4CkKrySm8V1cCebxnDmx53N Note the extra/p2p`.

Now it does connect when using bootstrapping and if send a pubsub message from that machine (so from 116.203.242.65 itself) then it arrives in the browser log. As i would expect.

But.. If i now use my local machine's IPFS (peer id: 12D3KooWHatJmgusU8E9WSnJiprkVALyLfUufbYsvcJxbzM8RqyD) to post a message on the same pubsub channel i get.... nothing.

I'm also not getting any additional nodes besides the one i'm connected to with the bootstrapping.

As for the examples. I will definitely help there with contributing this very example (use libp2p in the browser to connect to ipfs and use pubsub). As that, when working properly, is a very awesome tool to have!

So funnily enough, we're a lot further now but still on the very same issue that i started with. Not getting peers nor pubsub messages :) Oh well, we'll get there.

vasco-santos commented 3 years ago

I needed to use this address:

Yes, you need the /p2p part. I copied from your previous answer and I did not see it was missing. That is the multiaddr code for the peer id that will come afterwards.

I'm also not getting any additional nodes besides the one i'm connected to with the bootstrapping.

Yes, that would eventually be expected. If peer A cannot reach peer B by any way, it will not be able to propagate pubsub messages around. So, this seems a connectivity issue as before. If you do the manual dial from your local machine to the 116.203.242.65 it should work after. For not relying on these "manual discovery" and peers to discover each other out of the box, it really depends on what is your end goal. I am working on rendezvous and auto relay which will help a lot on this, but while we don't get there, it takes more effort to achieve such scenarios.

I recommend you describe what is your complete use case, and how you see the network topology and peers communication with each other. With that, I can help you with the best solution. Please create a post in the discuss forum for us to proceed.

markg85 commented 3 years ago

What i'm trying to achieve is basically a client-server model. I want a website (static with js, client side) to get a "signal" (from pubsub) to know if there is new content (just an ipfs hash) to download. The site then uses an ipfs gateway to get the content for that hash. This gateway can also be a web3 ipfs thingy. In my case i have a list of news items where the server sends me an update on pubsub about a new news item.

How i envision this to work is as follows: The client subscribes to pubsub on a predefined channel. The server (could be anywhere in the world) publishes new content on that same channel. The client receives it (an ipfs hash) and downloads it.

The only thing both know is the channel on which to publish messages.

You could apply the above principle to a whole slew of site concepts. Think of the following:

I could be wrong, but this seems to be a very valid and simple usecase for pubsub. One that works just fine if the browser is not involved. So where the client is an application that can connect via the IPFS api. Things just get exponentially more complicated as soon as a browser is involved.

The problem here is the client that somehow magically needs to connect to the same network as the rest of IPFS. That apparently proves to be a lot more complicated then i had though. Add to that the HTTPS demand of browsers nowadays and you're in a whole mess of protocols.

vasco-santos commented 3 years ago

Hey @markg85 Can you please proceed on posting this in https://discuss.libp2p.io instead? We are really out of the scope of libp2p-webrtc-star for a long time and this conversation should be open for the larger libp2p community so that other people can be involved on the discussion or at least learn from it later. I will be happy to answer you there, you can tag me. I will be closing this issue as there is nothing to be done on the webrtc stand point.

markg85 commented 3 years ago

Hey @markg85 Can you please proceed on posting this in https://discuss.libp2p.io instead? We are really out of the scope of libp2p-webrtc-star for a long time and this conversation should be open for the larger libp2p community so that other people can be involved on the discussion or at least learn from it later. I will be happy to answer you there, you can tag me. I will be closing this issue as there is nothing to be done on the webrtc stand point.

12 days later... sorry for that. I was expecting the IPFS discuss page... Are you sure i need to post on the libp2p one? I mean, the tech in the backend might be libp2p but from a user and even developer point of view this is going to be 100% IPFS.

I'll wait with posting till you confirmed this one :)

Thank you a lot for all your help thus far!

vasco-santos commented 3 years ago

@markg85 yes, it is also fine if you post to discuss.ipfs.io! Thanks for understanding

jacobfriedman commented 3 years ago

Bump - I think the autodialing isn't working.

    const libp2p = await Libp2p.create({
            peerId,
            addresses:      {
                    // add a listen address (localhost) to accept TCP connections on a random port
                    listen: [
                       '/ip4/0.0.0.0/tcp/0',
                        '/ip4/0.0.0.0/tcp/0/ws',
                        //`${peers.signallers[0]}`
                    ],
            },
            modules:        {
                    transport: [TCP/* WebRTCStar */],
                    connEncryption: [NOISE],
                    //streamMuxer: [MPLEX],
                    pubsub: GossipSub,
                    peerDiscovery: [ /*Bootstrap,*/ MulticastDNS],
                    //dht: KadeliaDHT
            },
                  peerDiscovery: {
                    mdns: {
                        interval: 60e3,
                        enabled: true
                      },
                  },
                  pubsub: {
                    enabled: true,
                    emitSelf: true,
                  },

          }
    })

...

/// This doesn't proc on discovery.
libp2p.on('peer:connect', (connection) => {
console.log('\n \n Connection established to:', connection.remotePeer.toB58String())    
})
jacobfriedman commented 3 years ago

Update... saw, after a google of Error: protocol selection failed... SECIO is required!? But it's deprecated! I added it and will post the update back here .

Not the greatest way to manage it but

      libp2p.peerStore.on('peer', async (peerId) => {
        console.log(`\n 🔭 Discovered:\t\t${peerId.toB58String()}`)
        await libp2p.dial(peerId);
            // ... do some stuff with the connection
      })
jacobfriedman commented 3 years ago

I might add - I'm trying to pass messages to myself through different instances locally.

jacobfriedman commented 3 years ago

Hrm. It looks like pubsub on a local computer/instance, between two nodeJS processes, only process one of the pubsub streams (i havent tested other topics yet).

vasco-santos commented 3 years ago

Hello @jacobfriedman Any reason for not creating a new issue? I am not sure if I understood the issue, but I noticed in your code an API issue so far:

libp2p.on('peer:connect', (connection) => {
console.log('\n \n Connection established to:', connection.remotePeer.toB58String())    
})

should be the follow (check the API.md):

libp2p.connectionManager.on('peer:connect', (connection) => {
console.log('\n \n Connection established to:', connection.remotePeer.toB58String())    
})

Update... saw, after a google of Error: protocol selection failed... SECIO is required!? But it's deprecated! I added it and will post the update back here .

So, if you are trying to talk with a node that unfortunately did not update its connectionEncryption module to Noise, your peer will not be able to talk with it as you will be using Noise and it only supports SECIO. What is the node in question?

jacobfriedman commented 3 years ago

I'll make a new issue. It's not an RTC problem. I added the connectionmanager in and will report on the pubsub issue I had

On Wed., Nov. 18, 2020, 4:05 a.m. Vasco Santos, notifications@github.com wrote:

Hello @jacobfriedman https://github.com/jacobfriedman Any reason for not creating a new issue? I am not sure if I understood the issue, but I noticed in your code an API issue so far:

libp2p.on('peer:connect', (connection) => {console.log('\n \n Connection established to:', connection.remotePeer.toB58String()) })

should be the follow (check the API.md https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#libp2pconnectionmanager ):

libp2p.connectionManager.on('peer:connect', (connection) => {console.log('\n \n Connection established to:', connection.remotePeer.toB58String()) })

Update... saw, after a google of Error: protocol selection failed... SECIO is required!? But it's deprecated! I added it and will post the update back here .

So, if you are trying to talk with a node that unfortunately did not update its connectionEncryption module to Noise, your peer will not be able to talk with it as you will be using Noise and it only supports SECIO. What is the node in question?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/libp2p/js-libp2p-webrtc-star/issues/268#issuecomment-729539641, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFKHXWOWAJMUOUGUAAHSMTSQOE7NANCNFSM4TFNY24A .