During the recent developer week, Cloudflare announced a Worker API to create outbound TCP sockets. The new socket API allows developers to connect back to TCP-based infra directly from a Worker, including databases.
Available as a Runtime API, the connect() function returns a TCP socket that allows developers to read and write data until the connection remains open. Workers could already interact with HTTP endpoints and other Cloudflare services, but the vast majority of databases require clients to connect by opening a direct TCP socket. Brendan Irvine-Broque, product manager at Cloudflare, and Matt Silverlock, director of product at Cloudflare, explain:
With Workers, we aim to support standard APIs that are supported across browsers and non-browser environments wherever possible, (...) but for TCP sockets, we faced a challenge — there was no clear shared standard across runtimes. We’ve tried to incorporate the best elements of existing APIs and proposals, and intend to contribute back to future standards.
Last autumn Cloudflare, together with Vercel and Shopify, started WinterCG, a new community group, focused on the interoperable implementation of standardized web APIs in non-web browser, javaScript-based development environments.
The new API is accessed by importing the connect function from cloudflare:sockets. One of the common use cases is to create a connection to a database, for example:
import { Client } from "pg";
export interface Env {
DB: string;
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const client = new Client(env.DB);
await client.connect();
const result = await client.query({
text: "SELECT * from customers",
});
console.log(JSON.stringify(result.rows));
const resp = Response.json(result.rows);
// Close the database connection, but don't block returning the response
ctx.waitUntil(client.end());
return resp;
},
};
Source: https://blog.cloudflare.com/workers-tcp-socket-api-connect-databases/
While pg, the JavaScript database driver for PostgreSQL, is already supported, the MySQL drivers mysql and mysql2 are not supported yet. Irvine-Broque and Matt Silverlock warn:
A new connection is created for every request. This is one of the biggest current challenges of connecting to databases from serverless functions, across all platforms (...) we’re already working on simpler approaches to connection pooling for the most popular databases.
The content delivery network expects to add more features in the future, including support for inbound TCP and UDP connections, as asked by some developers, as well as application protocols based on QUIC.
The connect() API was not the only new feature announced during the Developer Week 2023: Cloudflare introduced Secrets Store, a solution for managing application secrets securely, improvements to D1, Cloudflare’s serverless database, and consumer concurrency for the messaging service Queues. Furthermore, Cloudflare announced database integrations for Neon, PlanetScale, and Supabase on Workers. Karl Horky, founder at UpLeveled, tweets:
No proxy like Neon or other serverless/edge providers, you just connect normally over TCP. This sounds great, potentially way bigger than the other recent edge database announcements.
Each open TCP socket counts towards the maximum number of open connections that can be simultaneously open in Workers and TCP connections cannot be created on port 25 to send email to SMTP mail servers.