now_world
now_world

Reputation: 1096

Connect browser to NodeJS IPFS node

I'm trying to connect a browser js-ipfs node to a my NodeJS server that is running an js-ipfs node as well. I understand that I need to add in the server's Multiaddr to the browser's bootstrap config in order to connect the two nodes.

However, since my website uses HTTPS, when I try to connect the browser to my server via ws it gives this warning: An insecure WebSocket connection may not be initiated from a page loaded over HTTPS. but from my understanding NodeJS's js-ipfs cannot listen to wss calls.

So how can I go about connecting the two nodes? Is using WebSockets the only or best way?

Upvotes: 1

Views: 881

Answers (1)

vasco.santos
vasco.santos

Reputation: 206

Due to browser’s security policies you cannot establish unencrypted connection from secure context (e.g. page loaded via HTTPS). You can read about it at MDN Secure Context. So you should make your server to use SSL and then replace ws:// with wss://. The simplest thing to do would be to setup SSL with nginx. You’ll need to setup a domain name for the cert too. You can also look on some documentation on how to setup libp2p/IPFS with SSL on this PR.

You can also use webrtc-star instead of websockets. However, while you can use both, each one of them have particularities that are better for certain scenarios. If you need peer discovery, webrtc-star should be used at the moment and you can check https://github.com/libp2p/js-libp2p/tree/master/examples/libp2p-in-the-browser and https://github.com/ipfs/js-ipfs/tree/master/examples/browser-exchange-files . If you do not need to rely on peer discovery and the other peer is a well known peer you can use bootstrap and connect via websockets.

Upvotes: 2

Related Questions