Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/workers-vpc/configuration.md
1# Configuration23Setup and configuration for TCP Sockets in Cloudflare Workers.45## Wrangler Configuration67### Basic Setup89TCP Sockets are available by default in Workers runtime. No special configuration required in `wrangler.jsonc`:1011```jsonc12{13"name": "private-network-worker",14"main": "src/index.ts",15"compatibility_date": "2025-01-01"16}17```1819### Environment Variables2021Store connection details as env vars:2223```jsonc24{25"vars": { "DB_HOST": "10.0.1.50", "DB_PORT": "5432" }26}27```2829```typescript30interface Env { DB_HOST: string; DB_PORT: string; }3132export default {33async fetch(req: Request, env: Env): Promise<Response> {34const socket = connect({ hostname: env.DB_HOST, port: parseInt(env.DB_PORT) });35}36};37```3839### Per-Environment Configuration4041```jsonc42{43"vars": { "DB_HOST": "localhost" },44"env": {45"staging": { "vars": { "DB_HOST": "staging-db.internal.net" } },46"production": { "vars": { "DB_HOST": "prod-db.internal.net" } }47}48}49```5051Deploy: `wrangler deploy --env staging` or `wrangler deploy --env production`5253## Integration with Cloudflare Tunnel5455To connect Workers to private networks, combine TCP Sockets with Cloudflare Tunnel:5657```58Worker (TCP Socket) → Tunnel hostname → cloudflared → Private Network59```6061### Quick Setup62631. **Install cloudflared** on a server inside your private network642. **Create tunnel**: `cloudflared tunnel create my-private-network`653. **Configure routing** in `config.yml`:6667```yaml68tunnel: <TUNNEL_ID>69credentials-file: /path/to/<TUNNEL_ID>.json70ingress:71- hostname: db.internal.example.com72service: tcp://10.0.1.50:543273- service: http_status:404 # Required catch-all74```75764. **Run tunnel**: `cloudflared tunnel run my-private-network`775. **Connect from Worker**:7879```typescript80const socket = connect(81{ hostname: "db.internal.example.com", port: 5432 }, // Tunnel hostname82{ secureTransport: "on" }83);84```8586For detailed Tunnel setup, see [Tunnel configuration reference](../tunnel/configuration.md).8788## Smart Placement Integration8990Reduce latency by auto-placing Workers near backends:9192```jsonc93{ "placement": { "mode": "smart" } }94```9596Workers automatically relocate closer to TCP socket destinations after observing connection latency. See [Smart Placement reference](../smart-placement/).9798## Secrets Management99100Store sensitive credentials as secrets (not in wrangler.jsonc):101102```bash103wrangler secret put DB_PASSWORD # Enter value when prompted104```105106Access in Worker via `env.DB_PASSWORD`. Use in protocol handshake or authentication.107108## Local Development109110Test with `wrangler dev`. Note: Local mode may not access private networks. Use public endpoints or mock servers for development:111112```typescript113const config = process.env.NODE_ENV === 'dev'114? { hostname: 'localhost', port: 5432 } // Mock115: { hostname: 'db.internal.example.com', port: 5432 }; // Production116```117118## Connection String Patterns119120Parse connection strings to extract host and port:121122```typescript123function parseConnectionString(connStr: string): SocketAddress {124const url = new URL(connStr); // e.g., "postgres://10.0.1.50:5432/mydb"125return { hostname: url.hostname, port: parseInt(url.port) || 5432 };126}127```128129## Hyperdrive Integration130131For PostgreSQL/MySQL, prefer Hyperdrive over raw TCP sockets (includes connection pooling):132133```jsonc134{ "hyperdrive": [{ "binding": "DB", "id": "<HYPERDRIVE_ID>" }] }135```136137See [Hyperdrive reference](../hyperdrive/) for complete setup.138139## Compatibility140141TCP Sockets available in all modern Workers. Use current date: `"compatibility_date": "2025-01-01"`. No special flags required.142143## Related Configuration144145- **[Tunnel Configuration](../tunnel/configuration.md)** - Detailed cloudflared setup146- **[Smart Placement](../smart-placement/configuration.md)** - Placement mode options147- **[Hyperdrive](../hyperdrive/configuration.md)** - Database connection pooling setup148