Code Bug Fix: Upgrading WebSocket for the first time takes a long time with express + http-proxy-middleware

Original Source Link

I have a Graphql server with subscriptions enabled, when accessed directly from my client it works great. Now what I need to do is reroute the connection through my backend (an express server).

I have the following configuration on my server:

app.use(
  "/graphql",
  createProxyMiddleware({
    target: `http://${GQL_URL}`,
    changeOrigin: true,
    ws: true,
  })
);

This eventually works, however, the first connection when the server starts-up, which I can see in my console the message [HPM] Upgrading to WebSocket takes over five minute to complete.

By the time it manages to upgrade the connection, the client has already disconnected and retried multiple times and I get “instantly” all the stacked pending retries:

[server] [HPM] Upgrading to WebSocket
[server] [HPM] Client disconnected
[server] [HPM] Upgrading to WebSocket
[server] [HPM] Client disconnected
[server] [HPM] Upgrading to WebSocket
[server] [HPM] Upgrading to WebSocket
[server] [HPM] Client disconnected
[server] [HPM] Client disconnected
[server] [HPM] Upgrading to WebSocket
[server] [HPM] Client disconnected
[server] [HPM] Upgrading to WebSocket
[server] [HPM] Client disconnected
[server] [HPM] Upgrading to WebSocket

After that, the connections to the server work properly.

In principle this is not a major problem, as when I deploy, the application will not work for a few minutes and then start working, is not ideal but not all bad.

However, the major problem is when I am developing. Whenever I make any change to the servers code and it reloads I have to wait those few minutes to properly checkout if my application is working.

How can I fix this issue? Am I missing something?

Tagged : / / / /

Leave a Reply

Your email address will not be published. Required fields are marked *