DEV Community

Cover image for How to make your localhost accessible to the internet with ngrok
Ngan Kim Khong
Ngan Kim Khong

Posted on • Updated on

How to make your localhost accessible to the internet with ngrok

Because of some popular programming memes, I thought localhost is, well, only exist in your local machine. It's true, but on top of that, you can actually make it accessible to the internet, which means available to everyone, for some amount of time (not forever or anytime).

In order to do this, it's really simple, all you need to download ngrok . Here are the list of video tutorials from Twilio:
https://www.youtube.com/watch?v=oy13mDsXC4s ,
https://www.youtube.com/watch?v=S1uExj7mMgM ,
Only a few simple things you need to do.

And some articles about it:
https://www.freecodecamp.org/news/testing-webhooks-while-using-vagrant-for-development-98b5f3bedb1d/ ( don't worry about the virtual machine, you don't need one .)
https://medium.com/@veneciacalista/how-to-use-ngrok-for-react-84bf2acc3907

Well, you might wonder, why would you ever need to expose your local host to anyone? For me, I was trying to use an online service to send a POST request to my localhost database. Obviously the online service doesn't like that, it requires an URL that can be accessed by online tools, not some http://localhost:4000 , so, ngrok helped create a valid URL that can be accessed to my localhost, and by any requests from the internet.

It's kinda like allowing the world to see a corner of your bathroom. With you offer it and nobody really ... cares.

Perhaps you will never need to use it, but, for me, without knowing ngrok, I might take forever to figure out how to access to my current company's database which are complicated. I figured it would be hard to try it on my local machine. However it was really easy, a few reading documentations and boom it is done with ngrok.

Oldest comments (8)

Collapse
 
jesse1981 profile image
Jesse Bryant

This is not localhost tunneling. Just because you can access it locally doesn't mean anyone else can - even with this application.

It is only putting a sub domain to your public IP address. It is no different than if you were to give your friend your address being xxx.xxx.xxx.xxx/ to access it from your machine.

That isn't to say these applications aren't useful, to give you a fixed domain to an IP that changes each time you establish your connection.

Collapse
 
nk2303 profile image
Ngan Kim Khong

Oh, no? I thought anyone would have access to it, a third party app probably have, if I allow it running. I probably have to edit my note a little. I forgot to mention that the access only available for an amount of time. Thanks for the input! :)

Collapse
 
steventhan profile image
Steven Than

This is not localhost tunneling

Can you elaborate on this? ngrok is a tunneling tool which maintain a long-lived tcp connection between client (where the localhost lives) and ngrok's server, did I miss something?

It is only putting a sub domain to your public IP address

No, there's no public IP address on the user side here, only the ones ngrok manages

Collapse
 
jesse1981 profile image
Jesse Bryant

Sorry, I think there is still some confusion here;

Even if you uninstall ngrok, you always have a public IP address. You can google "whatsmyip" and it will show it to you, and you can use this address to go to the website being served from your machine.

Thread Thread
 
steventhan profile image
Steven Than

I'm aware each machine connecting to the internet has its own public IP address, but how is it relevant to this post?

When I said:

there's no public IP address on the user side here

I meant ngrok doesn't simply assign a sub domain to the machine's public IP address, it opens a tcp connection and keep it alive using a heartbeat mechanism . This is localhost tunneling in my book. By doing this, ngrok doesn't care if a machine's public IP is dynamic (which is most likely the case unless you explicitly request and pay additional fees to ISPs to obtain a static IP)

Thread Thread
 
jesse1981 profile image
Jesse Bryant

Ah, I see what you mean now.

Must admit, a description of localhost tunneling didn't make sense to me since the localhost was more like the endpoint (but not of course when the connection being initiated from the client), but the explanation on the link you shared of Stream Multiplexing made it clearer - neat idea.

FWIW, I personally used to use the solution if NoIP, same result, instead that application was just reporting your current IP to the service.

Collapse
 
varunpappu profile image
Varun Subramanian

Ngrok free version do come with limitations on the number of connections at a time. So beware on your requests failing at times.

Collapse
 
nk2303 profile image
Ngan Kim Khong

Gotcha. Yeah it was not something you'd provide to the internet I think. I was using just to try to get a few connections from the internet to my local machine. :) !