How access 'env' in the WebSockets Template

Hey, I've got a Cloudflare Worker that implements WebSocket connection. The implementation is from: https://github.com/cloudflare/websocket-template I am wondering how can I access the env variable that is usually available to us when we use the
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
...
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
...
syntax instead of the one used in the template,
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
B
Bonsaye15d ago
I want to access env so I can access MY_KV_NAMESPACE
C
Cyb3r-Jak315d ago
You'll want to convert the old syntax to not use the old service worker one
B
Bonsaye15d ago
I've tried but I can't get it to work. I hope I am wrong but I think you can't use the new syntax because its a WebSocket connection so if you define it with, the new syntax
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
..
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
..
it won't work as expected because, from the client-side when you invoke websocket.send(), I don't think that will invoke the fetch on the Worker
C
Cyb3r-Jak315d ago
I believe it should.
B
Bonsaye15d ago
hmm, you're right, it seems to be working - awaiting further confirmation lol
Want results from more Discord servers?
Add your server
More Posts
Durable object websocket connectionsI want to access the currently connected websockets to a durable object from outside the object itseHow to SSH Through a Tunnel?Hello CF Discord, - I have a local OpenSSH server running on an Ubuntu host. - I would like to useHugo Static Site missing images once deployed to Cloudflare PagesHi everyone, Thank you for looking at this issue. I made a simple Hugo website locally, git pushGCS ACLsGoogle Cloud Storage migration does not seem to work when using Buckets with Fine-grained access conAI multiple binary filesHi! I'm tried out the AI image generator, and it retrurns the image as a binary data. How can I sendpelican build integration is failingpelican build integration is failing because the pelican executable is either not installed or not oElevated number of 524Hey guys, we are seeing a lot of 524 responses, while the origin looks completely healthy. Any cluesonly "t" on response - AI Rest APIhey! I'm currently getting the following response on the second message to the AI: REST API responseEmail obfuscated when using HTMLRewriterHi, I'm using HTMLRewriter to parse some html data, when deployed I see parsed text has email obfusc"No deployment available" when deploying from Github ActionsMy setup is pretty straightforward. I have a Remix app with a simple build step (`remix vite:build`)Pages deployments failing (wrangler deploy)I'm attempting to deploy to an existing Remix (vite with cf) to Pages. Everything has been smooth unworkers-rs repo RA feature detectionDoes anyone else have the problem that rust-analyzer doesn't properly detect the enabled features inDynamically import incompatible packages for dev onlyI am currently dynamically importing a package that is incompatible in workers just during developmeDeploy by pushHello is it possible to deploy changes with git push like it's on vercel for my worker? or i have maAre the api docs for updating worker settings wrong?Specifically looking at this request: https://developers.cloudflare.com/api/operations/worker-scriptHow to access worker queue producer bindings in local development?At boot I see this message: ``` wrangler.toml changed... Using vars defined in .dev.vars Your worke