0

I have an MXChip which sends data to an Azure IoT Hub, from there I am using an Azure Function with Azure SignalR binding to post the device data to Azure Signal R. And I have an Angular client which will get the connection information by calling the Azure Negotiate function I had created, using the package @aspnet/signalr.

But the problem is my Angular client is throwing an error every few seconds, and when I check, I could understand that the hubConnection.onclose event is getting fired in every few seconds.

Below is my Angular service code.

export class SignalRService {
    mxChipData: Subject < string > = new Subject();
    private hubConnection: SignalR.HubConnection;

    constructor(private http: HttpClient) {}

    private getSignalRConnection(): Observable < SignalRConnection > {
        return this.http.get < SignalRConnection > (`${environment.baseUrl}negotiate`);
    }

    init() {
        this.getSignalRConnection().subscribe(con => {
            const options = {
                accessTokenFactory: () => con.accessToken
            };

            this.hubConnection = new SignalR.HubConnectionBuilder()
                .withUrl(con.url, options)
                .configureLogging(SignalR.LogLevel.Information)
                .build();

            this.hubConnection.on('notify', data => {
                this.mxChipData.next(data);
            });

            this.hubConnection.start()
                .catch(error => console.error(error));

            this.hubConnection.onclose((error) => {
                console.error(`Something went wrong: ${error}`);
            });
        });
    }
}

Is there any way I can get rid of this behavior?

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140

2 Answers2

2

I figured out an easy fix. The SignalR.HubConnection has the properties serverTimeoutInMilliseconds and keepAliveIntervalInMilliseconds.

serverTimeoutInMilliseconds

The server timeout in milliseconds.

If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error. The default timeout value is 30,000 milliseconds (30 seconds).

keepAliveIntervalInMilliseconds

Default interval at which to ping the server.

The default value is 15,000 milliseconds (15 seconds). Allows the server to detect hard disconnects (like when a client unplugs their computer).

I just set these values to greater numbers.

this.hubConnection.serverTimeoutInMilliseconds = 300000;
this.hubConnection.keepAliveIntervalInMilliseconds = 300000;

We can also start the Hub again in the onclose event as a temp fix.

this.hubConnection.onclose((error) => {
    this.hubConnection.start();
    console.error(`Something went wrong: ${error}`);
});
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
-1

Try switching your SignalR Service Mode to Serverless (or Classic)

Azure Portal SignalR Service Mode Setting

cigien
  • 57,834
  • 11
  • 73
  • 112