Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to manage graphql-ws client at the gateway for different users #193

Closed
quant-daddy opened this issue May 31, 2021 · 0 comments
Closed
Labels
question Further information about the library is requested

Comments

@quant-daddy
Copy link

In my architecture, each client (browser) connects to the gateway. The gateway then connects with the appropriate upstream graphql subscription service, creating a separate upstream websocket connection for each downstream connection using the code below

const clients: {
  [key: string]: { [userId: string]: Client };
} = {};

const getClient = (url: string, userId: string | null) => {
  let client: Client | null = null;
  if (!userId || !clients[url] || !clients[url][userId]) {
    client = createClient({
      url,
      webSocketImpl: ws,
      connectionParams: {
        userId: userId,
      },
      on: {
        closed: () => {
          console.log("closed url ", url);
        },
        connected: () => {
          console.log("connected url ", url);
        },
      },
    });
    // if a client is create for a user, the cache it
    if (userId) {
      clients[url] = {
        ...clients[url],
        [userId]: client,
      };
    }
  }
  // return the cached version for a user
  if (userId) {
    return clients[url][userId];
  }
  // for non users, return the generated client
  return client as Client;
};

What is recommended way to manage connections at scale: Can we use the same websocket connection for multiple downstream client connection? I think not. I have added more details in this issue. Would love to hear your thoughts @enisdenjo.

Repository owner locked and limited conversation to collaborators May 31, 2021
@enisdenjo enisdenjo added the question Further information about the library is requested label Jun 8, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Further information about the library is requested
Projects
None yet
Development

No branches or pull requests

2 participants