Using ngrok as an external process

I don’t know why it took me so long to figure this out but it turns out that the ngrok client has an api that you can access locally. That means you can run ngrok external to your process and then call to it to get the public tunnel information. This is very handy as ngrok doesn’t really like starting and stopping as rapidly as you may want while developing a server.

This means rather than using the ngrok package on npm for your local development, look towards docker hub:

https://hub.docker.com/r/wernight/ngrok

I added an entry to my docker-compose.yml file like so:

version: '3'
services:
  ngrok:
    image: wernight/ngrok
    ports:
      - 4040:4040
    environment:
      NGROK_PORT: 4000
      NGROK_PROTOCOL: http

Next in your code, after staring the containers, simply call the service from your application code:

const { status, data } = await axios.get('http://localhost:4040/api/tunnels')
const { tunnels } = data
const { public_url } = tunnels.find(t => t.proto === 'https')
this.publicEndpoint = public_url

The ngrok tunnel will stay alive as your server restarts over and over.