Skip to content
Discussion options

You must be logged in to vote

the handler itself is already the data itself

export async function GET({ platform, cookies }) {
  const session = cookies.get("session");
  const username = await getUsernameFromSession(session);
 
  return platform!.markForUpgrade(
    new Response('Websocket Required', {
      status: 400
    }),
    {
      username,
      open(ws) {
        console.log(ws.data === this); // => true
        ws.send(`Hello ${this.username}`); // use either this.username or ws.data.username, both work
        // even `Hello ${username}` would just work since username exists in this scope
      }
    }
  );
}

if you want to avoid unwanted variable kept in the scope, here is another approach

class Greeting {

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jarda-svoboda
Comment options

@eslym
Comment options

Answer selected by jarda-svoboda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #5 on July 28, 2025 01:37.