'Next.JS build with websocket connection

I'm trying to build a next.js application which uses signalr for to talk to webservices. However, during the build it tries to connect to the websocket which is not available at build-time and therefore the build fails with the following error:

FailedToNegotiateWithServerError: Failed to complete negotiation with the server: FetchError: request to https://localhost:7021/hubs/hub/negotiate?negotiateVersion=1 failed, reason: connect ECONNREFUSED 127.0.0.1:7021

How can we use websockets which are not available runtime. This is how we create the websocket connection in the app:

const socketInstance = new HubConnectionBuilder()
    .configureLogging(LogLevel.None)
    .withUrl(`${process.env.NEXT_PUBLIC_SOCKET_URL}`)
    .withAutomaticReconnect()
    .build();

socketInstance.start().then(() => {});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source