How to connect js-ipfs node on browser to go-ipfs node on remote server?

Hello, I encountered a problem and look forward to getting help:
I have 2 ipfs nodes, one is the js-ipfs daemon running on the browser, the other is the go-ipfs running on my remote server,
I used the code to start the js-ipfs daemon on the browser, it only runs on the browser, without installing any software or plug-ins,
I tried to connect the js-ipfs node on the browser to the go-ipfs node on my server, but it failed.

My js-ipfs starts like this:
const node = await Ipfs.create({ repo: userName });

The browser can be connected to the following nodes on the public network:
‘/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu’,

‘/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM’,

‘/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic’,

‘/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm’,

‘/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64’,

‘/dns4/node2.preload.ipfs.io/tcp/443/wss/p2p/QmV7gnbW5VTcJ3oyM2Xk1rdFBJ3kTkvxc87UFGsun29STS’,

‘/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3’,

‘/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd’,

‘/dns4/node3.preload.ipfs.io/tcp/443/wss/p2p/QmY7JB6MQXhxHvq7dBDh4HpbH29v4yE9JRadAVpndvzySN’,

‘/dns4/node1.preload.ipfs.io/tcp/443/wss/p2p/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6’,

But I can’t connect to my own server go-ipfs node, and can only use the form with the domain name above, not the form with IP,
For example:’/ip4/147.75.69.147/tcp/4001/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic’, this form is invalid.

I need help, I look forward to you helping me

Hello @enochzhao

Browser nodes do not support TCP transport, as the browser environment does not support it. In this scenario you described, you need to add listen addresses for you go-libp2p node using websockets and use the websocket transport to connect to it.

If you have a look into the DNS multiaddrs you provided, you can see that they are using websockets because of the /wss part.

Let me know if you could make this work

Hello, thank you very much for your reply,

I made the following configuration in config and used nginx for proxy forwarding, but no connection was successful,
I am a newbie, can you tell me the error in the configuration or operation process


ipfs config:

“Addresses”: {
“API”: “/ip4/127.0.0.1/tcp/5001”,
“Announce”: [],
“Gateway”: “/ip4/127.0.0.1/tcp/8080”,
“NoAnnounce”: [],
“Swarm”: [
“/ip4/0.0.0.0/tcp/4001”,
“/ip6/::/tcp/4001”,
“/dns4/domain-name/tcp/443/wss”
]
},


nginx config:

server {
listen 443;
server_name domain-name;
ssl on;

    ssl_certificate   /etc/nginx/cert/ipfs/a.pem;
    ssl_certificate_key  /etc/nginx/cert/ipfs/a.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location /websocket {

   		proxy_pass http://ip.ip.ip.ip:4001;
	proxy_set_header Host $host;
	proxy_http_version 1.1;
 	proxy_set_header Upgrade $http_upgrade;    
 		proxy_set_header Connection "Upgrade";
	proxy_set_header X-Real-IP $remote_addr;
        		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        		proxy_connect_timeout 60;
       		proxy_read_timeout 600;
       		proxy_send_timeout 600; 
}

}
server{
listen 80;
server_name domain-name;
rewrite ^(.*) https://{server_name}$1 permanent;
}

If I understood correctly your flow, you want to establish a connection from a js-ipfs node to a go-ipfs node that you already know its multiaddr. My answer will have this scenario in account.

For this scenario, I recommend that you use the default configuration (https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/src/core/runtime/config-browser.js) as you do not need to listen for new connections in the browser node. It will be the starter party for establishing a connection.

As I mentioned, we cannot use TCP within the browser. Your configuration seem all TCP oriented and go-ipfs listens on TCP by default. This way, what I think you need is to add a websocket listen address in the go node (Some information: https://github.com/ipfs/go-ipfs/blob/b7f03a56bbde9ba1bc0c930673cf7483cdbb2e6a/docs/transports.md).

Considering a different scenario, where you actually also want your node to listen for connections from other parties. If this is your end goal, you should at the js-ipfs repo browser examples, and also to this thread on SSL certificates for webrtc-transport https://discuss.libp2p.io/t/cant-connect-to-webrtc-star-with-ssl/522

Hope that this helps you

Thank you, great, I used the method in this link https://github.com/ipfs/go-ipfs/blob/b7f03a56bbde9ba1bc0c930673cf7483cdbb2e6a/docs/transports.md, the browser connects to the go-ipfs node on the remote server Succeeded,
As you said, I implemented this function: the remote server go-ipfs node can listen to the connection of other nodes as well as the connection of the browser node. Thank you for your help! ! !

1 Like