Keeping Ngrok TCP Tunnels Alive: Automatic Reconnection Explained
Learn how Ngrok’s agent‑level keep‑alive and automatic reconnection keep TCP tunnels alive across brief network outages, with a concrete SSH‑tunnel example and verification steps.
03 Jul 2026, 10:48 UTC

Problem: Long‑running TCP connections drop when the network hiccups
If you run an IoT device, a remote database client, or any service that relies on a persistent TCP socket through Ngrok, a brief loss of connectivity (e.g., Wi‑Fi roaming, cellular handover, or a router reboot) can cause the tunnel to close. The client then sees a broken socket and must restart the Ngrok command manually, which adds operational overhead and risks data loss.
Thesis: Ngrok’s built‑in keep‑alive and reconnection logic lets a TCP tunnel survive short network outages without manual intervention
Starting with agent version 3.x, Ngrok monitors the underlying connection and automatically re‑establishes the tunnel when the link returns. From the perspective of the client application, the socket appears stable; only a brief pause may occur while the agent negotiates a new connection.
How the mechanism works
- The Ngrok agent sends periodic TCP keep‑alive frames (configurable via the underlying Go runtime) to detect a dead peer.
- When a keep‑alive fails, the agent marks the tunnel as
offlinelocally and begins a reconnection attempt. - Upon successful reconnection, the agent updates the tunnel status to
onlineand forwards traffic as if nothing changed. - All of this happens inside the Ngrok binary; the client never sees a socket closure, only a possible latency spike.
Worked example: persisting a TCP tunnel for a remote SSH service
Suppose you want to expose port 22 on a Raspberry Pi to the internet via a stable TCP tunnel that survives network blips.
- Create a configuration file (e.g.,
~/ngrok.yml) that defines the tunnel and enables detailed logging: - Start Ngrok pointing to the config file:
- Monitor reconnection events in real time:
- Verify from the client side: Keep an SSH session open; after the network returns, the session should resume without a
Connection reset by peererror. You may notice a brief pause (typically a few hundred milliseconds) while Ngrok renegotiates.
version: "2"
authtoken: YOUR_NGROK_AUTHTOKEN
tunnels:
ssh:
proto: tcp
addr: 22
remote_addr: yoursubdomain.tcp.ngrok.io:0 # 0 lets Ngrok assign a random port
inspect: true
Run this file on the device with sufficient permissions to bind to port 22 (usually root or via sudo for privileged ports).
# Run in a terminal or as a service
sudo ngrok start --config ~/ngrok.yml ssh
Ngrok will output lines similar to:
Session Status online
Version 3.x.x
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding tcp://yoursubdomain.tcp.ngrok.io:12345 -> localhost:22
Connection established
# Add --log=stdout to see live logs
sudo ngrok start --config ~/ngrok.yml ssh --log=stdout
When you temporarily disable the network interface (e.g., sudo ifconfig eth0 down), you will see log entries like:
[WARN] keepalive failed: read tcp 10.0.0.2:54321->ngrok.io:443: i/o timeout
[INFO] reconnecting tunnel ssh...
[INFO] connection reestablished
Trade‑offs and limitations
- Latency impact: During reconnection, data flow pauses. For latency‑sensitive protocols (e.g., real‑time gaming or high‑frequency trading), this pause may be unacceptable.
- Quota dependence: Automatic reconnection attempts consume tunnel quotas. If your Ngrok account lacks sufficient concurrent tunnels, reconnection will fail and the tunnel stays down until quota is freed or upgraded.
- No application‑level awareness: The client cannot distinguish a brief Ngrok‑induced pause from a genuine server outage; higher‑level retry logic may still be needed for critical transactions.
Practical way to check the result
After starting the tunnel as shown above:
- Open a local inspector:
http://127.0.0.1:4040and watch the tunnel status toggle betweenonlineandofflinewhile you intermittently disable/enable the network interface. - Check the terminal output (or log file) for the sequence
Connection established→Reconnecting→Connection reestablished. - From another machine, maintain a long‑running TCP test (e.g.,
openssl s_client -connect yoursubdomain.tcp.ngrok.io:12345 -quiet) and verify that the stream resumes after the network recovers.
Closing: Use Ngrok’s built‑in resilience for simpler operations
For many edge‑computing, remote‑admin, or IoT scenarios, Ngrok’s automatic TCP reconnection removes the need for custom keep‑alive scripts or external process managers. By configuring a tunnel once (via ngrok.yml) and enabling verbose logging, you gain visibility into the reconnection process while benefiting from a stable socket appearance. Keep in mind the brief pause during reconnection and ensure your account has enough tunnel quota to sustain the attempts. With those considerations addressed, you can rely on Ngrok to keep your TCP links alive through everyday network fluctuations.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.