Setting up websockets with caddy so I can connect go-ipfs with js-ipfs

I’m trying to setup a gateway that exposes my node to a js-ipfs browser based, so that I can reliably send pub-sub messages. I find that unless I connect to the same peers the pubsub messages get lost. I’m trying to use docker and caddy. My question is basically, how do I configure the websocket connection on the go-ipfs side, and what is the initial swarm that I’ll need on the browser js-ipfs side?

Here’s the current setup. Test machine name is ssb.willschenk.com:

docker create network identity

start_ipfs.sh:

IPFSDATA=${PWD}/ipfs_data
IPFSEXPORT=${PWD}/ipfs_export
docker run --name ipfs \
        -d --restart unless-stopped \
        -p 4001:4001 \
        -p 127.0.0.1:8080:8080 \
        -p 127.0.0.1:5001:5001 \
        --network identity \
        -v ${IPFSDATA}:/data/ipfs \
        -v ${IPFSSTAGING}:/export \
        -m 900m\
        ipfs/go-ipfs \
        daemon --writable --enable-pubsub-experiment

start_caddy.sh

CADDYFILE=${PWD}/Caddyfile

docker run --name caddy \
        -d --restart unless-stopped \
        -p 80:80 -p 443:443 \
        --network identity \
        -v ${PWD}/html:/html \
        -v ${CADDYFILE}:/etc/Caddyfile \
        -v ${ACCESSLOG}:/src/access.log \
        -v caddy_keys:/root/.caddy \
        -e ACME_AGREE=true \
        -e HOSTNAME=${HOSTNAME} \
        abiosoft/caddy

Caddyfile:

{$HOSTNAME}
root /html
gzip
cors
cache
log
errors error.log

proxy /ipfs/ http://ipfs:8080/ 

proxy /ws http://ipfs:8081 {
        websocket
}

And then I’ve set inside of the config file:

  "Addresses": {
    "API": "/ip4/0.0.0.0/tcp/5001",
    "Announce": [],
    "Gateway": "/ip4/0.0.0.0/tcp/8080",
    "NoAnnounce": [],
    "Swarm": [
      "/ip4/0.0.0.0/tcp/4001",
      "/ip4/0.0.0.0/tcp/8081/ws",
      "/ip6/::/tcp/4001"
    ]
  },

And my node test code:

const IPFS = require('ipfs')

const node = new IPFS({
  config: {
    Addresses: {
      Swarm: [
        '/dns4/ssb.willschenk.com/tcp/443/wss/p2p-websocket-star'
      ]
    }
  }
})

console.log( "Connecting" )
node.on('ready', async () => {
  const version = await node.id()

  console.log( version )

  console.log( "Peers" )

  const peers = await node.swarm.peers()

  console.log(peers)
})

So my questions are:

  1. What am I doing wrong? :slight_smile:
  2. What is the right path to proxy from Caddy to the ipfs websocket server? I’m using /ws on my public facing proxy server to proxy to the 8081 port?
  3. What should my initial swam address be in the node side? I’m using '/dns4/ssb.willschenk.com/tcp/443/wss/p2p-websocket-star' but it doesn’t show any peers or error messages?
  4. How do I debug js-ipfs better? Why is it silently starting?
  5. Is there something special I need to do to enable p2p-websocket-star on the go-ipfs side?

Been banging my head against this but there are too many things I don’t understand to move forward… Thanks in advance.

I think you should use / for the websocket path in Caddy. Do you see anything in (Caddy) logs?

As far as I’m aware p2p-websocket-star is a separate service that go-ipfs doesn’t provide (seems to be https://github.com/libp2p/js-libp2p-websocket-star-rendezvous)

I think that you should use /dns4/ssb.willschenk.com/tcp/443/wss as a bootstrap peer (I’m not sure how that’s handled in js-ipfs). You should be also ablo to call ipfs.swarm.connect('/dns4/ssb.willschenk.com/tcp/443/wss') and see that connection in browser.

If you want the browser node to be able to talk to the outside, try following https://github.com/ipfs/js-ipfs/tree/master/examples/circuit-relaying

OK so I never figured it out with caddy, but I wrote up how to do it with docker-compose, nginx and certbot https://willschenk.com/articles/2019/setting_up_an_ipfs_node/