ipAddress
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
This Val Town project provides a robust service to help a React Native mobile app discover and connect to a restaurant server running on a desktop application, even when network conditions are challenging.
In a restaurant management system where:
- A desktop application runs the server with APIs
- A React Native mobile app needs to connect to those APIs
- The server's IP address might change or be difficult to determine
- The mobile app and server might be on different networks
This service acts as a bridge that allows the server to register its current IP addresses and the mobile app to discover and test connectivity to the server.
- Server Registration: When the desktop server starts, it sends both its local and public IP addresses to this service
- Mobile App Discovery: The mobile app queries this service to get the latest server IP addresses
- Connectivity Testing: The mobile app tests connectivity to both the local and public IPs to determine which one works
- Direct Connection: The mobile app then connects directly to the server using the working IP
POST /register
Authorization: Bearer YOUR_SECRET_TOKEN
Content-Type: application/json
{
"ipAddress": "192.168.1.100",
"port": 8080,
"serverName": "MainRestaurantServer",
"allIpAddresses": ["192.168.1.100", "10.0.0.15"]
}
GET /
Response:
{ "localIpAddress": "192.168.1.100", "publicIpAddress": "203.0.113.10", "port": 8080, "serverName": "MainRestaurantServer", "updatedAt": "2023-06-15T12:34:56.789Z" }
GET /list
GET /client-ip
POST /test
Content-Type: application/json
{
"testIp": "192.168.1.100",
"testPort": 8080
}
- Uses Val Town's blob storage to persist server information
- Stores both local and public IP addresses for each server
- Includes basic token authentication for the registration endpoint
- Provides CORS headers to allow cross-origin requests from the mobile app
- Includes troubleshooting endpoints to help diagnose connectivity issues
- Set the
SERVER_REGISTER_TOKEN
environment variable in Val Town for security - Configure the desktop server to call the registration endpoint on startup
- Configure the React Native app to use the provided example code
If you're having connectivity issues:
- Check Network: Make sure both devices are on the same network
- Firewall Settings: Ensure the server's firewall allows incoming connections on the API port
- IP Address: Use the manual connection option in the React Native app to try different IP addresses
- Port Forwarding: If connecting from outside the local network, set up port forwarding on your router
- VPN Issues: Disable any VPNs that might interfere with local network discovery
The /examples
directory contains:
server-registration.js
- Example code for the desktop server to register its IPreact-native-client.js
- Example React Native component with robust connectivity handling