Connect to local servers
The hosted simulator cannot reach the localhost of your computer, and your computer cannot reach the localhost of the machine. See How it works. sim-remote provides two tunnel commands, one for each direction.
Reverse tunnels
A reverse tunnel lets the simulator reach a server on your computer. The app in the simulator connects to localhost:8081, and the traffic arrives at port 8081 on your computer. A local Metro bundler, a mock API or a staging proxy works with a hosted simulator in this way.
sim-remote reverse start <UDID> 8081
Argent opens a reverse tunnel for Metro and for the debugger on its own. See Argent.
Forward tunnels
A forward tunnel lets your computer reach a server on the hosted machine. sim-remote binds a local port and sends the traffic to a port on the machine. A local tool can talk to a test runner that runs next to the simulator in this way. Only servers that your own session started are reachable.
sim-remote forward start <UDID> 9000:8000
The hosted simulators on one machine share the network interfaces of that machine. Only one simulator on the machine can bind a given port, across all users. A server that binds a fixed port fails to start when another simulator already holds that port.
Bind an ephemeral port in the server: request port 0 and read the port that the system assigns. Then forward to the assigned port. sim-remote does not forward to a port that the server of another user holds.
Most apps connect to a service and do not listen on TCP, so most apps need a reverse tunnel only. A forward tunnel is common with test runners. For example, an agent in the simulator serves HTTP and drives the app through XCTest.
Lifetime
The background daemon owns the tunnels. A tunnel stays open between commands and closes when the session ends or when the session moves to another machine.
Example instructions
- "Expose my Metro server on port 8081 to the hosted simulator."
- "Show the tunnels that are open now."
- "Close the tunnel on port 8081."
The CLI reference lists the reverse and forward commands.