0
# Network Configuration
1
2
Network setup utilities for React Native development, including Metro bundler port forwarding and reverse proxy configuration for device connectivity.
3
4
## Capabilities
5
6
### ADB Reverse Port Forwarding
7
8
Sets up reverse port forwarding to allow Android devices to connect to the Metro development server running on the development machine.
9
10
```typescript { .api }
11
/**
12
* Set up reverse port forwarding for Metro bundler
13
* Runs 'adb reverse tcp:8081 tcp:8081' to allow loading the JS bundle from the packager
14
* @param packagerPort - Port number where Metro bundler is running
15
* @param device - Optional specific device identifier for reverse forwarding
16
*/
17
function tryRunAdbReverse(packagerPort: number | string, device?: string | void): void;
18
```
19
20
**Usage Examples:**
21
22
```typescript
23
import { tryRunAdbReverse } from "@react-native-community/cli-platform-android";
24
25
// Set up reverse forwarding for default Metro port
26
tryRunAdbReverse(8081);
27
28
// Set up reverse forwarding for custom port
29
tryRunAdbReverse(8082);
30
31
// Set up reverse forwarding for specific device
32
tryRunAdbReverse(8081, "emulator-5554");
33
34
// Use string port number
35
tryRunAdbReverse("8081");
36
```
37
38
## Network Configuration Details
39
40
### Port Forwarding Mechanism
41
42
The `tryRunAdbReverse` function establishes a reverse TCP connection that allows Android devices and emulators to access the Metro development server running on the host machine. This is essential for React Native development as it enables:
43
44
- **Hot Reloading**: Live updates during development
45
- **Remote Debugging**: DevTools integration
46
- **Bundle Loading**: JavaScript bundle serving from Metro
47
- **WebSocket Connection**: Real-time communication between device and Metro
48
49
### Default Port Configuration
50
51
By default, React Native's Metro bundler runs on port 8081. The reverse port forwarding maps:
52
- Device port 8081 → Host port 8081
53
54
This allows React Native apps running on Android devices to access `http://localhost:8081` and reach the Metro server on the development machine.
55
56
### Error Handling
57
58
The function includes built-in error handling for common scenarios:
59
- **ADB not found**: Gracefully handles missing ADB installation
60
- **Device not connected**: Handles disconnected or unavailable devices
61
- **Port already in use**: Manages port conflicts
62
- **Permission issues**: Handles ADB permission problems
63
64
**Important Notes:**
65
66
- This function is automatically called by the `run-android` command
67
- Manual invocation is useful for custom development setups
68
- The reverse forwarding persists until the device is disconnected or ADB server restarts
69
- Multiple devices can have reverse forwarding configured simultaneously