Dev tips: Remotely kill an ngrok session

ngrok is an awesome product.

We use it here at Fomo, but as a remote team, one problem we have from time to time is a team member leaving a shared ngrok connection open, blocking anyone else from using it.

Tunnel session failed: The tunnel 'https://fomo.ngrok.io' is already 
bound to another tunnel session

We could upgrade our account to allow more connections, but that would not solve our issue because most our configurations are hard-coded to our specific ngrok subdomain (fomo.ngrok.io, no touché).

Our solution

Create an endpoint on our web app to trigger a system command to notify the team member and/or kill the ngrok process.

We use Rails. Here is our simple route / controller configuration:

# routes.rb
get '/ngrok_notify', to: 'ngrok_notify#notify' if Rails.env.development?

# ngrok_notify_controller.rb
def notify
  system("osascript -e 'display notification \"\" with title \"Fomo - ngrok\" subtitle \"We turned off ngrok - #{params[:performer] ? params[:performer] : 'someone else' } needs it!\"'")

  system('killall ngrok') if params[:force]

  render html: ’notified’
end

Now a team member requiring ngrok can just go to any browser and hit https://fomo.ngrok.io/ngrok_notify?performer=chris to send a mac system notification to the team member using ngrok.

If the current team member is still using ngrok, they can let the team know via Slack ping. If not, then hit https://fomo.ngrok.io/ngrok_notify?force=1 to kill all ngrok processes (for when your team member goes to sleep and you just started your work day!).

Happy hacking!

**big props to Klemen for putting together this solution!