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))
})
5 Replies
NazCodeland
NazCodelandOP9mo ago
I want to access env so I can access MY_KV_NAMESPACE
Cyb3r-Jak3
Cyb3r-Jak39mo ago
You'll want to convert the old syntax to not use the old service worker one
NazCodeland
NazCodelandOP9mo 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
Cyb3r-Jak3
Cyb3r-Jak39mo ago
I believe it should.
NazCodeland
NazCodelandOP9mo ago
hmm, you're right, it seems to be working - awaiting further confirmation lol
Want results from more Discord servers?
Add your server