Always getting the error parameter as 'undefined' in an onclose event of the SignalR Typescript client.

vsi_pranav 156 Reputation points
2021-01-05T15:03:03.78+00:00

Hello,
I am trying to implement simple chat application using SignalR. For front-end, I am using Angular 10 with Typescript SignalR client. For this client I am using the latest @microsoft/signalr package. While the back-end server is in .Net Core 3.1 WebAPI project. Also, I am using the Azure SignalR service with Standard pricing tier.
I am referring the following documentation.
https://learn.microsoft.com/en-us/javascript/api/@microsoft/signalr/hubconnection?view=signalr-js-latest#onclose--error---error-----void-
As per the documentation, the 'onclose' event receives an optional error parameter if there is any. But in my case in the client code, I am always getting this error as 'undefined'. I want to use this error parameter to determine if the connection was closed purposefully by client or was there any error either from client or server and based on that I want to take the further action in code. Find below my code snippet.

this.hubConnection.onclose((error) => {  
  // Try to log the error.  
  if (typeof error !== 'undefined') {  
    console.log(this.TAG + '(onclose) Error: ' + error.name + ' : ' + error.message);  
  } else {  
    console.log(this.TAG + '(onclose) No error to show while closing the connection.');  
  }  

  // Log the connection state.  
  console.log(this.TAG + '(onclose) HubConnection State: ' + this.hubConnection.state);  
});  

Kindly please take a look at this and let me know if I am doing it correctly or is there any other way to do this. Also, if you need more details about the issue, please let me know.

Thanks.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,618 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
147 questions
{count} votes

Accepted answer
  1. Ryan Hill 28,716 Reputation points Microsoft Employee
    2021-01-05T22:33:46.807+00:00

    Hi @vsipranav-0854,

    As you stated, error is an optional parameter for onclose and is only populated when connection is closed due to an actual error. The code snippet you provided seems perfectly fine to me.

    Regards,
    Ryan


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.