Skip to content

Invalid (or missing) WebSocket implementation with SSR (server-side rendering) #215

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

You must be logged in to vote

The issue here is that the client is being instantiated during server-side rendering on the server. Because the browser's WebSocket is not available there, this issue appears.

Recommended solution, as suggested by @praful-chandra in #215 (reply in thread), is to skip over WebSocket parts when not in a browser environment.

const wsLink = typeof window === 'undefined' ? null : new WebSocketLink({
  url: "ws://localhost:4000/graphql",
  connectionParams: () => {
    const accessToken = getAccessToken().valueOf();

    if (!accessToken) return {};

    return {
      Authorization: accessToken ? `Bearer ${accessToken}` : '',
    };
  },
});

process.browser can be used for checking the environ…

Replies: 3 comments 15 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
15 replies
@praful-chandra
Comment options

@enisdenjo
Comment options

@furkandmrblk
Comment options

@furkandmrblk
Comment options

@enisdenjo
Comment options

Comment options

You must be logged in to vote
0 replies
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
3 participants
Converted from issue

This discussion was converted from issue #214 on July 21, 2021 08:28.