Skip to content

How to unsubscribe from server side #373

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

You must be logged in to vote

Same in case of authentication changed, need to completely disconnect the socket?

I always recommend to completely kick off the client for re-authentication. It will have the client re-run the connecting phase allowing the server to properly authorise. Once the client reconnects, it will resubscribe all previously active subscriptions.

Assuming you're using the built-in servers, the socket is always available in the context's extras. I recommend closing with the 4403: Forbidden close code, but you're free to close with any non-fatal close code you wish. For example, with graphql-ws/lib/use/ws:

import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import {

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by namnm
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 #372 on June 20, 2022 11:25.