Being a developer also comes with the endless pursuit of making work easier for yourself. Luckily the rest of the developer world is also in on this pursuit and has often done just that by creating handy tools. Most tools only solve parts of your problem and just save you a little bit of time and frustration. However, once in a while you find a tool that’s such a perfect fix for your situation! So great it will make you want to run around like a maniac going door to door spreading the word of this cool new tool you’ve just found. This time around that tool was Ngrok for testing webhooks.
A webhook is a pretty simple concept. When you’re working with a third party and want to be notified about certain changes, the other party can supply custom callback about events like status changes. You then supply them an URL where you want to receive a postback, so you can act on these events. Now, this sounds easy to do and shouldn’t be much of a problem, so why would you want to use Ngrok? The answer: for testing this on your local machine!
Usually, your localhost or docker container isn’t (or you don’t want it to be) accessible from outside sources. You could do a lot of configuration and open ports on which the webhook posts its callback or add some exceptions in your firewall. But chances are you don’t want to be bothered by this every time you are going to implement a webhook. This is where Ngrok helps out by creating a secure tunnel to your localhost for testing the webhooks. It’s very easy! All you have to do download and unzip Ngrok, go to the folder in your terminal and type ./ngrok HTTP and the port you want the tunnel to use.
This will result in two generated URLs for HTTP and HTTPS which u can set as the URL you want to receive the webhook postback on. The URL will remain active as long as the terminal is open so when you close the terminal, you don’t have to worry about the tunnel staying open after testing your webhooks.
Ngrok also provides a very handy web UI on localhost port 4040 ( http://localhost:4040) where u can view the incoming requests.
The web UI can also replay the request so you don’t have to wait for the webhook to fire of again, which speeds things up a lot.
As a closing remark, I would like to point out that although Ngrok is made for implementing webhooks. I could see myself using it for other purposes like during mobile development for easy access to localhost from emulator or mobile device. Or when I need to quickly test a responsive web app on localhost from a tablet.
By: Rowan van Balen
Thanks for this nice read.
For people using docker, this works out of the box. All I had to do was adding these lines to my docker-compose.yml
ngrok:
image: wernight/ngrok
links:
– webserver
command: “ngrok http webserver:80”
Where webserver is the container of the application that processes the webhook.