How I got an external service to talk to my local Rails server

We’re currently on a project where we had to integrate Twilio. One of the services Twilio provides is a way for clients to call a phone number that reroutes back to your web application for further instructions. When I first got started, the first challenge was figuring out how to develop with Twilio and get it to talk back to my local development Rails server.

The first solution I went with was Localtunnel. It was pretty easy to install the gem and run it. You basically call:

$ localtunnel 3000

Then Localtunnel provides an external URL you can insert for your callback URLs in your Twilio account.

$ localtunnel 3000
Port 3000 is now accessible from ...

This was pretty cool, except for the bit where I had to constantly update the callback URL on my Twilio account every time I restarted Localtunnel. I had to find a better way.

The solution I settled on was remote SSH port forwarding. I had originally gone down this path but the part that had tripped me up was that by default, remote port forwarding is disabled. To enable it, you have add the following line on the forwarding server’s /etc/ssh/sshd_config file:

GatewayPorts yes

Once you make the change and restart sshd, you can run the remote port forwarding command:

ssh -R 3333:localhost:3000

What this means is that the local process on port 3000 (Rails) can be accessed via port 3333 on the port forwarding server’s address. That is, is “rerouted” to http://localhost:3000.

Here’s a simple diagram explaining how the whole thing works (local port 8080, remote port 8888).

remote ssh port forwarding

Related resources you may find useful:

That’s all folks!

Post a Comment

Your email is never shared. Required fields are marked *