|
|
hace 3 días | |
|---|---|---|
| .. | ||
| README.md | hace 3 días | |
| api.md | hace 3 días | |
| configuration.md | hace 3 días | |
| gotchas.md | hace 3 días | |
| patterns.md | hace 3 días | |
Connect Cloudflare Workers to private networks and internal infrastructure using TCP Sockets.
Workers VPC connectivity enables outbound TCP connections from Workers to private resources in AWS, Azure, GCP, on-premises datacenters, or any private network. This is achieved through the TCP Sockets API (cloudflare:sockets), which provides low-level network access for custom protocols and services.
Key capabilities:
Note: This reference documents the TCP Sockets API. For the newer Workers VPC Services product (HTTP-only service bindings with built-in SSRF protection), refer to separate documentation when available. VPC Services is currently in beta (2025+).
Need private network connectivity from Workers?
| Requirement | Use | Why |
|---|---|---|
| HTTP/HTTPS APIs in private network | VPC Services (beta, separate docs) | SSRF-safe, declarative bindings |
| PostgreSQL/MySQL databases | Hyperdrive | Connection pooling, caching, optimized |
| Custom TCP protocols (SSH, MQTT, proprietary) | TCP Sockets (this doc) | Full protocol control |
| Simple HTTP with lowest latency | TCP Sockets + Smart Placement | Manual optimization |
| Expose on-prem to internet (inbound) | Cloudflare Tunnel | Not Worker-specific |
Use TCP Sockets when you need:
Don't use TCP Sockets when:
fetch() or VPC Services)import { connect } from 'cloudflare:sockets';
export default {
async fetch(req: Request): Promise<Response> {
// Connect to private service
const socket = connect(
{ hostname: "db.internal.company.net", port: 5432 },
{ secureTransport: "on" }
);
try {
await socket.opened; // Wait for connection
const writer = socket.writable.getWriter();
await writer.write(new TextEncoder().encode("QUERY\r\n"));
await writer.close();
const reader = socket.readable.getReader();
const { value } = await reader.read();
return new Response(value);
} finally {
await socket.close();
}
}
};
Most private network connectivity combines TCP Sockets with Cloudflare Tunnel:
┌─────────┐ ┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Worker │────▶│ TCP Socket │────▶│ Tunnel │────▶│ Private │
│ │ │ (this API) │ │ (cloudflared)│ │ Network │
└─────────┘ └─────────────┘ └──────────────┘ └─────────────┘
See configuration.md for Tunnel setup details.
| Limit | Value |
|---|---|
| Max concurrent sockets per request | 6 |
| Blocked destinations | Cloudflare IPs, localhost, port 25 |
| Scope requirement | Must create in handler (not global) |
See gotchas.md for complete limits and troubleshooting.