You can use a Cloudflare Worker as the origin of a Spectrum TCP application. Spectrum accepts the public TCP connection and invokes the Worker's connect() handler with a bidirectional socket.
Worker origins support use cases such as:
- Routing bidirectional protocols, including gRPC, to a Cloudflare Container.
- Exposing existing TCP applications in Containers without rewriting them as HTTP services.
- Running MQTT, multiplayer game, and custom binary protocol servers in Containers.
- Receiving device, telemetry, or syslog data over TCP and sending it to Cloudflare storage or queues.
- Implementing lightweight TCP services directly in Workers.
Before creating the Spectrum application:
- Add a domain to Cloudflare and ensure Cloudflare is authoritative for its DNS.
- Use a Pro or Business zone, or a zone with Spectrum Enterprise enabled. Free zones are not supported. Spectrum Enterprise access is separate from a zone's Enterprise plan.
- In the same account as the zone, deploy a Worker that exports a
connect()handler. - For API and CLI configuration, find the Worker's immutable 32-character tag. The tag is different from the Worker name.
Without Spectrum Enterprise, Pro and Business zones support one Worker-origin application per zone, on one TCP port. This application is in addition to existing plan-supported applications, such as SSH or Minecraft. Refer to Protocols per plan for availability.
Review your Spectrum subscription charges and Workers pricing before deploying. Workers usage is billed separately from Spectrum.
The setup in this guide passes TCP traffic through without terminating TLS. Requests without Spectrum Enterprise must omit the tls field. To encrypt the connection, terminate TLS in the service behind the Worker, or use Spectrum Enterprise TLS termination.
Choose the dashboard, Cloudflare CLI, or API procedure.
The dashboard steps depend on whether the zone has Spectrum Enterprise.
Pro or Business zone without Spectrum Enterprise
-
In the Cloudflare dashboard, go to Spectrum.
Go to Spectrum ↗ -
Select Create an Application.
-
Under Application Configuration, select Workers (tcp) as the Application Type. This option is unavailable if the zone already has a Worker-origin application.
-
In Subdomain, enter the subdomain that clients will connect to. The dashboard adds your zone name after it.
-
In Edge Port, enter a single TCP port.
-
In Origin Worker, select the Worker that will receive the connections. The list shows deployed Workers that export a
connect()handler. -
Select Add Application.
These applications have no TLS or advanced options. Spectrum passes the TCP payload to the Worker without terminating TLS.
Zone with Spectrum Enterprise
-
In the Cloudflare dashboard, go to Spectrum.
Go to Spectrum ↗ -
Select Create an Application.
-
Under Application Configuration, select TCP as the Application Type.
-
In Subdomain, enter the subdomain that clients will connect to. The dashboard adds your zone name after it.
-
In Edge Port, enter a single TCP port.
-
Under Origin Configuration, select Worker as the Origin type.
-
In Origin Worker, select the Worker that will receive the connections. The list shows deployed Workers that export a
connect()handler. -
Leave Edge TLS Termination turned off to pass the TCP payload to the Worker without terminating TLS.
-
Select Add Application.
-
To find the Worker tag, search for the deployed Worker:
npx cf@latest workers scripts search --name <WORKER_NAME>yarn dlx cf@latest workers scripts search --name <WORKER_NAME>pnpx cf@latest workers scripts search --name <WORKER_NAME>Copy the Worker ID, also called the Worker tag, from the result.
-
Create the Spectrum application:
npx cf@latest spectrum apps create --zone example.com --body '{"dns":{"type":"CNAME","name":"tcp.example.com"},"protocol":"tcp/50051","traffic_type":"worker","origin_worker_id":"<WORKER_TAG>"}'yarn dlx cf@latest spectrum apps create --zone example.com --body '{"dns":{"type":"CNAME","name":"tcp.example.com"},"protocol":"tcp/50051","traffic_type":"worker","origin_worker_id":"<WORKER_TAG>"}'pnpx cf@latest spectrum apps create --zone example.com --body '{"dns":{"type":"CNAME","name":"tcp.example.com"},"protocol":"tcp/50051","traffic_type":"worker","origin_worker_id":"<WORKER_TAG>"}'Replace the zone, hostname, port, and Worker tag with your values. The explicit
--bodysends only the fields supported without Spectrum Enterprise. Additional CLI defaults, such astlsorproxy_protocol, require Spectrum Enterprise.
-
Find the Worker tag with the List Workers endpoint:
At least one of the following token permissions is required:Required API token permissions
Workers Tail ReadWorkers Scripts WriteWorkers Scripts ReadWorkers AdminWorkers EditorWorkers Content Read-OnlyWorkers Metadata Read-OnlyIndividual Workers AdminIndividual Workers EditorIndividual Workers Content Read-OnlyIndividual Workers Metadata Read-Only
List Worker Scriptsbash curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/scripts" \ --request GET \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"In the response, find the Worker by its
idand copy itstag. -
Create the Spectrum application:
At least one of the following token permissions is required:Required API token permissions
Zone Settings Write
Create Spectrum application using a name for the originbash curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/spectrum/apps" \ --request POST \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "dns": { "type": "CNAME", "name": "tcp.example.com" }, "protocol": "tcp/50051", "traffic_type": "worker", "origin_worker_id": "<WORKER_TAG>" }'
Spectrum applications with a Worker origin have the following constraints:
- This TCP beta uses a single port per application. Port ranges are not supported.
traffic_typemust beworker.origin_worker_idmust contain the target Worker's tag, not its name.- On Pro and Business zones without Spectrum Enterprise, omit
tls. Spectrum does not terminate TLS for these applications. - With Spectrum Enterprise,
tlscan beofforflexible. - Do not set
origin_direct,origin_dns,origin_port,virtual_network_id, Proxy Protocol, or Argo Smart Routing.
Refer to Configuration options for details.
To test the plaintext echo handler, run:
nc tcp.example.com 50051Enter a message and confirm the Worker echoes it. If your service terminates TLS behind the Worker, use that service's TLS client instead.
The Spectrum application sends the connection to the Worker's connect() handler. Review the Worker's real-time logs to confirm the handler ran.
Spectrum Enterprise applications can use tls: "flexible" to terminate TLS at Cloudflare. Spectrum sends the decrypted TCP payload to the Worker. This mode requires an active advanced certificate or custom certificate covering the Spectrum hostname. Universal SSL is not supported.
In the dashboard, turn on Edge TLS termination and select Flexible for TLS mode. For API or CLI configuration, add "tls":"flexible" to the request body. Do not include this field unless Spectrum Enterprise is enabled.
To test the echo handler with Spectrum TLS termination, run:
openssl s_client -connect tcp.example.com:50051 -servername tcp.example.com -quietFor an end-to-end bidirectional gRPC example, refer to Bidirectional gRPC with Workers and Containers.