question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hub handshake failed with error 'WebSocket is not in the OPEN state' SignalR WebSockets Angular client with AutoReconnect()

See original GitHub issue

My application was first built on dotnetcore 2.2, then it was upgraded to dotnetcore 3.0 and it was working properly. But WithAutoReconnect() from Angular web app and all the default settings, gives following log:

[2019-12-05T12:10:14.538Z] Information: Reconnect attempt number 3 will start in 10000 ms.
zone.js:3372 OPTIONS http://127.0.0.1:9938/host/negotiate?clientIdentifier=b462ed0d-4df0-4f2d-82b1-4ad1ee72b09b net::ERR_CONNECTION_REFUSED
Utils.js:208 [2019-12-05T12:10:24.540Z] Debug: Starting connection with transfer format 'Text'.
Utils.js:208 [2019-12-05T12:10:24.540Z] Debug: Sending negotiation request: http://127.0.0.1:9938/host/negotiate?clientIdentifier=b462ed0d-4df0-4f2d-82b1-4ad1ee72b09b.
Utils.js:208 [2019-12-05T12:10:24.683Z] Debug: Selecting transport 'WebSockets'.
Utils.js:208 [2019-12-05T12:10:24.683Z] Trace: (WebSockets transport) Connecting.
Utils.js:204 [2019-12-05T12:10:24.723Z] Information: WebSocket connected to ws://127.0.0.1:9938/host?clientIdentifier=b462ed0d-4df0-4f2d-82b1-4ad1ee72b09b&id=KOrhcPrFdlHaeTzcJxZjlA.
Utils.js:208 [2019-12-05T12:10:24.724Z] Debug: The HttpConnection connected successfully.
Utils.js:208 [2019-12-05T12:10:24.724Z] Debug: Sending handshake request.
Utils.js:208 [2019-12-05T12:10:24.743Z] Debug: Hub handshake failed with error 'WebSocket is not in the OPEN state' during start(). Stopping HubConnection.
Utils.js:208 [2019-12-05T12:10:24.744Z] Trace: (WebSockets transport) socket closed.
Utils.js:208 [2019-12-05T12:10:24.744Z] Debug: HttpConnection.stopConnection(undefined) called while in state Disconnecting.
Utils.js:198 [2019-12-05T12:10:24.744Z] Error: Connection disconnected with error 'WebSocket is not in the OPEN state'.
Utils.js:208 [2019-12-05T12:10:24.745Z] Debug: HubConnection.connectionClosed(WebSocket is not in the OPEN state) called while in state Reconnecting.
Utils.js:204 [2019-12-05T12:10:24.745Z] Information: Reconnect attempt failed because of error 'WebSocket is not in the OPEN state'.
Utils.js:204 [2019-12-05T12:10:24.745Z] Information: Reconnect attempt number 4 will start in 30000 ms.
Utils.js:208 [2019-12-05T12:10:54.747Z] Debug: Starting connection with transfer format 'Text'.
Utils.js:208 [2019-12-05T12:10:54.748Z] Debug: Sending negotiation request: http://127.0.0.1:9938/host/negotiate?clientIdentifier=b462ed0d-4df0-4f2d-82b1-4ad1ee72b09b.
Utils.js:208 [2019-12-05T12:10:54.773Z] Debug: Selecting transport 'WebSockets'.
Utils.js:208 [2019-12-05T12:10:54.773Z] Trace: (WebSockets transport) Connecting.
Utils.js:204 [2019-12-05T12:10:54.780Z] Information: WebSocket connected to ws://127.0.0.1:9938/host?clientIdentifier=b462ed0d-4df0-4f2d-82b1-4ad1ee72b09b&id=JEu_f9AcM9fx4OE3SMWEVg.
Utils.js:208 [2019-12-05T12:10:54.780Z] Debug: The HttpConnection connected successfully.
Utils.js:208 [2019-12-05T12:10:54.781Z] Debug: Sending handshake request.
Utils.js:208 [2019-12-05T12:10:54.781Z] Trace: (WebSockets transport) sending data. String data of length 32.
Utils.js:204 [2019-12-05T12:10:54.782Z] Information: Using HubProtocol 'json'.
Utils.js:208 [2019-12-05T12:10:54.829Z] Trace: (WebSockets transport) data received. String data of length 3.
Utils.js:208 [2019-12-05T12:10:54.830Z] Debug: Server handshake complete.
Utils.js:204 [2019-12-05T12:10:54.831Z] Information: HubConnection reconnected successfully.

The server is disconnected and client tries to reconnect. When server goes up and connected; then the following steps occurred:

  1. 3rd retry attempted and though “The HttpConnection connected successfully.” but “Hub handshake failed with error ‘WebSocket is not in the OPEN state’ during start(). Stopping HubConnection” and (WebSockets transport) socket closed. HttpConnection.stopConnection(undefined) called while in state Disconnecting. And as result “Error: Connection disconnected with error ‘WebSocket is not in the OPEN state’.” is logged in the console.
  2. After 30sec 4th retry attempted and It will automatically reconnected. These steps are consistency reproducible.

Please help me to get rid of this error in between 3rd and 4th retry attempt.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:28 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
BrennanConroycommented, Jan 21, 2020

Thanks, we can reproduce the issue and will look into the problem.

1reaction
yentheocommented, Jan 16, 2020

I have the same issue as described above, the first retry fails immediately with the message ‘WebSocket is not in the OPEN state’. I changed my code to the following:

    // build connection with retry policy
    const connection = new HubConnectionBuilder()
      .withUrl(url, options)
      .withAutomaticReconnect(new InlineRetryPolicy(context => {
        // the first retry fails with this message, but it is unclear why, if I retry immediatly after this, the connection succeeds
        if (context.retryReason.message === 'WebSocket is not in the OPEN state') {
          return 0;
        } else {
          observer.next({ state: HubConnectionState.Disconnected });
          // when callback is called, start emitting state changes with the remaining seconds untill retry
          RETRY_STREAM.pipe(map(x => ({ state: HubConnectionState.Reconnecting, seconds: RETRY_IN_SECONDS - x - 1 }))).subscribe(x => observer.next(x));
          // return the amount of milliseconds the signalr library has to wait
          return RETRY_IN_SECONDS * 1000;
        }
      }))
      .build();

So when that is the fail message, I reconnect directly, otherwise I do my normal retry logic, I don’t like hacks/fixes like this, but I don’t really know where the issue lies

Read more comments on GitHub >

github_iconTop Results From Across the Web

SignalR Asp.netcore and angular ( WebSocket is not in the ...
NET Core 2.2 and Angular 8 with package "@aspnet/signalr": "^1.1.4" . I have set up a hub in .NET Core 2.2 which works...
Read more >
Hub handshake failed with error 'WebSocket is not in the ...
Hub handshake failed with error 'WebSocket is not in the OPEN state' SignalR WebSockets Angular client with AutoReconnect()
Read more >
ASP.NET Core SignalR connection troubleshooting
This error is usually caused by a client using only the WebSockets transport but the WebSocket protocol isn't enabled on the server. Response ......
Read more >
Build Real-time Applications with ASP.NET Core SignalR
For most common scenarios, a client only needs to reconnect to the hub when a connection is lost or a connection attempt fails....
Read more >
How to Implement SignalR Automatic Reconnect with ...
Here, we can see that initially the WebSocket connection is established and our client app gets real-time data from the server application. Then ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found