Websocket to Container
Forwarding a Websocket request to a Container
WebSocket requests are automatically forwarded to a container using the defaultfetch
method on the Container class:
import { Container, getContainer } from "@cloudflare/workers-types";
export class MyContainer extends Container {  defaultPort = 8080;  sleepAfter = "2m";}
export default {  async fetch(request, env) {    // gets default instance and forwards websocket from outside Worker    return getContainer(env.MY_CONTAINER).fetch(request);  },};Additionally, the containerFetch method can be used to forward WebSocket requests as well.
Was this helpful?
- Resources
 - API
 - New to Cloudflare?
 - Products
 - Sponsorships
 - Open Source
 
- Support
 - Help Center
 - System Status
 - Compliance
 - GDPR
 
- Company
 - cloudflare.com
 - Our team
 - Careers
 
- 2025 Cloudflare, Inc.
 - Privacy Policy
 - Terms of Use
 - Report Security Issues
 - Trademark