WebSocket connection to 'ws: //127.0.0.1: 43361 Error in connection establishment

Hello @mathtick

When you start an IPFS node for the first time (if you use a new repo this will be the case), your IPFS node will start to connect to the peers it discovers over time. When peers are discovered, their discovered multiaddrs are stored in the PeerStore and a connection is attempted to that multiaddr. If a connection is achieved with a peer, they will run the libp2p identify protocol, which exchanges their advertising multiaddrs (among other things). The PeerStore is updated with all the multiaddrs the peer advertises for future needs.

When you restart a peer (reusing a previously created repo), it will start by trying to connect to the previously known peers, in order to setup its network topology. The dialer grabs several known multiaddrs for each peer (previously stored) and tried to connect with the peer with all of them in parallel, in order to achieve a connection faster.
With the flows detailed, let’s go deeper in what the problem of the websocket connection established errors in the browser. Some nodes on the network are advertising local websockets addresses, which makes peers storing these addresses and in the future trying to use them to dial. Currently, browser security policies do not allow to dial such websocket addresses, and only wss addresses. Consequently, the browser will throw that error in the console, but the connection will eventually be established from one of the other multiaddrs attempted.

We have been working on ways of improving this situation over the last months. Nodes can configure the addresses they do not want to advertise in the network, but this requires manual configuration and not an out of the box solution, which results in peers advertising them. The new libp2p 0.30 release has some goodies that help a lot with this. More specifically, the dialer will now prioritize public addresses by default when getting a few multiaddrs of a peer to try. In addition, libp2p now allows an easier way of not advertising private addresses. Finally, and the most important aspect, it comes with libp2p-websockets 0.15 release. This will completely get rid of this error. With this version, an IPFS node running in the browser will not attempt to dial any address unless it is a DNS+WSS address.

This will probably be released on the next js-ipfs release. You can track the progress in the integration PR https://github.com/ipfs/js-ipfs/pull/3427

2 Likes