Skip to content

How to get webscoket context in onClose #462

Answered by enisdenjo
alucardu asked this question in Q&A
Discussion options

You must be logged in to vote

You can use the context extra field which is shared between the hooks for distinct connections:

const serverCleanup = useServer(
  {
    schema,
-   onConnect: async () => {
+   onConnect: async (ctx) => {
+     ctx.extra.userName = getUserName()
      console.log('User connected')
    },
-   onClose() {
+   onClose(ctx)
      console.log('User disconnected')
      userResolvers.Mutation.deleteUser({
-       name: 'John'
+       name: ctx.extra.userName
      })
    },
  },
  wsServer
)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@alucardu
Comment options

@enisdenjo
Comment options

Answer selected by enisdenjo
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