Skip to content

How to close the websocket connection from client side with apollo link #134

Answered by enisdenjo
edwardfeng-db asked this question in Q&A
Discussion options

You must be logged in to vote

Hmm, this would require a combination of a graceful restart and timing the retryWait client option. In essence, something like this:

import { createClient } from './client';

// will be reassigned once the client connects
let gracefullyRestart = () => {
  // call me when the tab loses focus
};

const client = createClient({
  url: 'wss://graceful.restart/is/a/non-fatal/close-code',
  retryWait: () =>
    new Promise((resolve) => {
      // resolve me when the tab gets focus
    }),
  on: {
    connected: (socket) => {
      gracefullyRestart = () => {
        if (socket.readyState === WebSocket.OPEN) {
          socket.close(4205, 'Client Restart');
        }
      };
    },
  },
});

// …

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@enisdenjo
Comment options

@edwardfeng-db
Comment options

@enisdenjo
Comment options

@edwardfeng-db
Comment options

@enisdenjo
Comment options

Answer selected by edwardfeng-db
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