Connect browser to NodeJS IPFS node

I’m running js-ipfs on both the browser and on my NodeJS server. I would like to connect the browser’s ipfs node to my server on page load so that I can start to retrieve data faster from the server.

I’ve seen answers that say I need to open up a 443 port and listen for wss data then forward that data to the IPFS node. However, my server is the same server that hosts my application so 443 is already bound to my NodeJS Fastify https server.

What’s the best way to go about connecting my browser to my server’s IPFS node? Is WebSockets the only or recommended way to do this?

443 is just the default port for an HTTPS endpoint. If your URL starts with https:// your browser and the server assume to use port 443. However you can still use a different port but you’ll just need to include the port on the URL when you do that. Seems like you can just configure ipfs to run on some other port.

1 Like

That makes sense. 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 js-ipfs cannot listen to wss calls. So how should I go about this?

I’ve never actually done WebSockets from a browser (or with IPFS) or else I’d try to help.

1 Like

You can configure your node.js node to listen on arbitrary addresses using the Addresses.Swarm config key, eg change 4003 to something else below:

  Addresses: {
    // other config
    Swarm: [
      // other addresses
  // other config

You’ll then need to configure nginx or some other service as per the link in your original post to proxy to that port and do SSL termination for the domain you’re hosting the node on so the node can listen on plain ws: and not wss:.

The reason is, in order to run js-IPFS in the browser you need the web crypto API, which browsers only make available if the page is served over HTTPS. If the page is served over HTTPS the browser will stop you from connecting to non-wss:// websockets.

The actual port numbers are completely arbitrary, no need to use 443.

1 Like