Ever wondered how services like ngrok and Cloudflare Tunnel let the entire internet access an application running on your laptop, even when your router blocks incoming connections?

Let's understand what's actually happening behind the scenes.

Imagine you're developing a Node.js application on your laptop.

You start your development server:

npm run dev 

Your application is now running at:

http://localhost:3000 

You can access it from your browser. Your phone might access it over the same Wi-Fi network.

But what if you want someone sitting on the other side of the world to access this application?

Your first thought might be:

"Why don't I just share my IP address?"

And that's where networking gets interesting.

1. Why Can't Someone Directly Access Your Computer?

When you visit a website, your browser initiates a connection to a server.

For example:

Your Browser ──────────> Website Server HTTP Request 

The server receives your request and sends back a response.

But when you try to reverse this process, things become complicated.

Your personal computer is usually sitting behind a home router.

Your network might look something like this:

 INTERNET | | Public IP Address | HOME ROUTER | NAT / DHCP | 192.168.1.10 | YOUR PC | localhost:3000 

Your computer generally has a private IP address, such as 192.168.1.10.

Your router has a public-facing address and performs something called NAT (Network Address Translation).

NAT allows multiple devices in your home to share one public IPv4 address.

When your computer makes an outgoing request, the router creates a mapping so that incoming response packets can reach the correct device.

But what happens when someone from the internet tries to initiate a completely new connection to your computer?

Your router doesn't automatically know which internal device should receive it.

It may simply drop the request.

And there's another complication: your ISP might use CGNAT, meaning even your router doesn't have its own dedicated public IPv4 address.

So traditional port forwarding might not work at all.

Here's the interesting observation:

Your computer can usually initiate connections to the internet even when the internet cannot initiate connections to your computer.

And this is the fundamental idea behind tunnelling.

2. The Big Idea: What If Your Computer Calls the Server First?

Let's introduce two components:

  1. Tunnel Client: A small program running on your computer.
  2. Tunnel Server: A publicly accessible server operated by a tunnelling provider.

Instead of waiting for an external computer to connect to yours, your computer establishes an outbound connection to the tunnel provider.

The connection remains alive.

 YOUR COMPUTER TUNNEL PROVIDER +-------------+ +-------------+ | | | | | Tunnel |===============>| Tunnel | | Client | Outbound | Server | | | Connection | | +-------------+ +-------------+ | | localhost:3000 Your Application 

Why does this work?

Because your computer initiated the connection.

Your router allows the outgoing connection and maintains a NAT mapping for it.

The tunnel provider can send data back over that already-established connection.

We haven't opened a new inbound connection to your home network. We've reused an existing outbound connection.

That's the trick.

3. Let's Follow an Actual Request Through the Tunnel

Suppose you are running your application on:

localhost:3000 

You start Cloudflare Tunnel or ngrok.

The provider gives you a public URL:

https://my-app.example.com 

Now someone sitting in another country opens that URL.

What happens?

Let's follow the complete journey.

Step 1: Your computer establishes the tunnel

The tunnel client connects to the provider's infrastructure.

It generally uses an outbound encrypted connection, such as TLS over TCP or QUIC.

The client maintains the connection and can reconnect if it gets interrupted.

Step 2: Someone visits your public URL

A remote user opens:

https://my-app.example.com 

DNS resolves the domain to the provider's public infrastructure.

The request reaches the provider's reverse proxy.

Step 3: The provider identifies your tunnel

The provider already knows that your computer established a tunnel.

It has routing information that associates your public hostname with the appropriate tunnel and local service.

Conceptually:

Public hostname Tunnel Local service
app.example.com Tunnel A localhost:3000
api.example.com Tunnel B localhost:8000
admin.example.com Tunnel C localhost:5000

These are illustrative mappings. Actual implementations vary.

Step 4: The request travels through the existing connection

The provider forwards the request through the connection your computer previously established.

It doesn't need to create a fresh inbound connection through your home router.

Step 5: Your local tunnel client forwards the request

Your tunnel client receives the request and makes a local connection to:

localhost:3000 

Your Node.js application receives an ordinary HTTP request.

It doesn't necessarily know that the original user was thousands of kilometres away.

Step 6: The response travels back

Your application generates a response.

The tunnel client receives it and sends it through the established tunnel connection.

The provider delivers it to the remote user.

Here's the complete picture:

 REMOTE USER | | HTTPS Request | v +---------------+ | | | Tunnel Server | | | +---------------+ | | Existing Connection (Tunnel established by your computer) | | v +---------------+ | | | Tunnel Client | | | +---------------+ | | localhost:3000 | v +---------------+ | | | Node.js App | | | +---------------+ | | HTTP Response | v REMOTE USER 

And just like that, your local development server is accessible through a public endpoint.

4. But How Does the Tunnel Server Know Where to Send the Request?

This is another interesting part.

Imagine thousands of developers are running tunnels simultaneously.

How does the provider know which developer should receive which request?

When your tunnel client connects, it establishes its identity with the provider.

The provider maintains tunnel registration and routing information.

For example:

Tunnel ID: developer-laptop-01 Public URL: app.example.com Connected Client: developer-laptop-01 Local Destination: localhost:3000 Status: Connected 

When a request arrives for app.example.com, the provider identifies the correct tunnel and forwards the request through its active connection.

This is essentially reverse proxying combined with persistent outbound connections.

The tunnel server acts as the publicly accessible entry point, while the tunnel client acts as the bridge to your local application.

5. What Actually Travels Through the Tunnel?

Here's where networking becomes even more interesting.

A tunnel doesn't magically transport your entire computer.

It transports data over an established connection.

For example, imagine a remote browser sends this HTTP request:

GET /users HTTP/1.1 Host: app.example.com 

The tunnel infrastructure carries the relevant request data to your local tunnel agent.

The agent forwards it to your application.

Depending on the implementation, tunnel services may support:

  • HTTP request forwarding.
  • TCP connections.
  • Multiplexing multiple requests over one connection.
  • Encryption between tunnel endpoints.
  • Connection health checks.
  • Automatic reconnection.
  • Load balancing across multiple tunnel connections.

What is multiplexing?

Imagine multiple users accessing your application simultaneously.

Instead of necessarily establishing a completely separate tunnel for every request, a protocol such as HTTP/2 can multiplex multiple streams over one underlying connection.

Conceptually:

 ONE TUNNEL CONNECTION | +------------+------------+ | | | v v v Request 1 Request 2 Request 3 Stream 1 Stream 2 Stream 3 | | | +------------+------------+ | TUNNEL CLIENT 

This is conceptual. The exact transport and multiplexing strategy depends on the tunnel implementation.

6. Is Tailscale the Same as Cloudflare Tunnel?

Not exactly.

Both help you access machines that aren't directly reachable from the public internet, but they solve somewhat different problems.

Cloudflare Tunnel / ngrok

These primarily expose a service through a public or controlled endpoint.

For example:

Internet | v Public URL | v Tunnel Provider | v Your Tunnel Client | v localhost:3000 

You can share a URL with someone and allow them to access your application, subject to your access controls.

Tailscale / WireGuard

These are more focused on private networking.

Imagine you have:

  • A MacBook running your backend.
  • A phone.
  • Another laptop.

You want these devices to communicate as if they were connected to the same private network.

Tailscale uses WireGuard-based encrypted networking and coordinates connections between devices.

It attempts direct peer-to-peer connectivity using NAT traversal.

If a direct connection isn't possible, Tailscale can relay encrypted traffic through its DERP infrastructure.

 PRIVATE NETWORK +-------------------+ | | | TAILSCALE | | | +-------------------+ / \ / \ v v +----------+ +----------+ | MacBook | | Phone | | | | | +----------+ +----------+ 

The important distinction:

Cloudflare Tunnel is primarily about exposing services through a tunnel.

Tailscale is about connecting devices through a private network.

7. Can We Build Our Own Tunnel?

Absolutely.

And honestly, this is one of the best ways to understand networking beyond just using libraries and frameworks.

Let's imagine building a miniature version of ngrok using Node.js.

We need two applications:

A. Public Server

This runs on a publicly reachable machine.

Responsibilities:

  • Accept incoming HTTP requests.
  • Maintain a registry of connected tunnel clients.
  • Identify which tunnel should receive a request.
  • Forward requests through the appropriate connection.
  • Return responses to the original requester.

B. Local Tunnel Agent

This runs on your laptop.

Responsibilities:

  • Establish a persistent connection to the public server.
  • Register itself with a unique tunnel ID.
  • Receive forwarded requests.
  • Send requests to local services.
  • Return responses through the tunnel.

Our architecture:

 REMOTE BROWSER | | GET /hello | v +----------------------+ | | | PUBLIC SERVER | | | | Reverse Proxy | | Tunnel Registry | | | +----------------------+ | | WebSocket Channel (Persistent) | | v +----------------------+ | | | LOCAL TUNNEL AGENT | | | +----------------------+ | | localhost:3000 | v +----------------------+ | | | YOUR NODE.JS APP | | | +----------------------+ 

We could use:

Component Technology
Public server Node.js
Tunnel connection WebSocket
Request forwarding HTTP
Tunnel identification Unique tunnel ID
Local application Express.js
Encryption TLS

One important point: WebSocket is a convenient way to prototype this concept, but production tunnel implementations need additional engineering around concurrent requests, authentication, reconnection, timeouts, flow control and security.

8. Let's Actually Experiment

I think the best way to understand this is to build it.

Here is the progression I'd follow:

  1. Create an Express server running on localhost:3000.
  2. Create another Node.js server on a publicly reachable machine.
  3. Make your laptop establish a persistent WebSocket connection to that server.
  4. Make the public server receive an HTTP request.
  5. Forward the request through the existing WebSocket connection.
  6. Let your local agent forward it to Express.
  7. Send the response back through the WebSocket connection.

If everything works, you'll have created your own basic reverse tunnelling system.

No ngrok. No Cloudflare Tunnel. Just your own implementation of the underlying concept.

Final Thoughts

The thing that fascinated me most about this concept is that we don't necessarily need to make our computer directly reachable from the internet.

We can make our computer initiate a connection to a machine that is already publicly reachable, keep that connection alive, and use it to exchange data in both directions.

We aren't necessarily opening a door from the internet into our computer. We're establishing a communication channel from our computer and using that channel to receive requests.

That's the fundamental idea behind reverse tunnelling.

And once you understand this, a lot of networking concepts start making more sense.

I'd love to hear from developers who have implemented tunnelling, NAT traversal or reverse proxies themselves.

What happens inside these systems that you think every backend developer should understand?