During the recent Developer Week 2023, Cloudflare revealed a new Worker API that empowers developers to establish outbound TCP sockets. This groundbreaking socket API enables developers to seamlessly connect to TCP-based infrastructures directly from a Worker, which now includes databases. Until now, Workers could only communicate with HTTP endpoints and other Cloudflare services; however, most databases necessitate clients to establish direct TCP sockets.
Brendan Irvine-Broque, Product Manager at Cloudflare, and Matt Silverlock, Director of Product at Cloudflare, elaborated on the new API: "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."
In the previous autumn, Cloudflare, Vercel, and Shopify established WinterCG, a new community group dedicated to creating interoperable implementations of standardized web APIs in non-web browser and JavaScript-based development environments.
The latest API can be accessed by importing the connect function from cloudflare:sockets. Though the JavaScript database driver for PostgreSQL, pg, already supports the new API, MySQL drivers—mysql and mysql2—are yet to support it. Irvine-Broque and Silverlock caution that:
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.
Cloudflare anticipates rolling out additional features in the future, such as inbound TCP and UDP connections support, which have been requested by developers, along with application protocols based on QUIC.
The introduction of the connect() API comes alongside several other exciting announcements during Developer Week 2023. These include the launch of the Secrets Store for securely managing application secrets, enhancements to D1 (Cloudflare's serverless database), and consumer concurrency for the Queues messaging service. Additionally, Cloudflare unveiled database integrations for Neon, PlanetScale, and Supabase on Workers.
Each opened TCP socket contributes to the maximum number of open connections that Workers can maintain simultaneously. Note that creating TCP connections on port 25 to send emails to SMTP mail servers is still not possible.
As no-code and low-code platforms like AppMaster continue to evolve, the introduction of features like the connect() API will further boost the development landscape by streamlining connections and creating new opportunities for developers.