Search code examples
react-nativesignalrasp.net-core-signalr

How to intercept, in react-native, the signalR error 'Error: Server timeout elapsed without receiving a message from the server.'?


I'm using in react-native the package @aspnet/signalr to connect with my server.

All work correctly until the app is in foreground, I'm able to reconnect if I lose the connection without receiving errors.

When I open the app after a long time in in background I can reconnect immediately to my server but I receive the error Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'

How can I intercept this error?

This is a piece of my code:

connection = new signalR.HubConnectionBuilder()
   .withUrl("http://192.168.xxx.xxx/notificationHub?userId=" + authInfo.userId)
   .build();
    
connection.on("receiveMessage", data => {
   console.log('*** MESSAGGIO RICEVUTO ***');
   Alert.alert(data);
});


                        
connection.start()
   .then(() => console.log("Connessione avvenuta"))
   .catch((err) => console.log(err);

connection.onclose()
   .then(() => connection.start(););

Thanks


Solution

  • Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'

    The default timeout value of serverTimeoutInMilliseconds is 30,000 milliseconds (30 seconds), if this timeout elapses without receiving any messages from the server, the connection might be terminated with above error.

    To troubleshoot the issue, please check if you just update KeepAliveInterval setting of your SignalR hub but not change the serverTimeoutInMilliseconds value on your client side.

    And the recommended serverTimeoutInMilliseconds value is double the KeepAliveInterval value.

    Update:

    Is there a way to intercept this error and manage the error without warning?

    If you do not want the signalR client log this error in browser console tab, you can try to modify the LogLevel to None.

     .configureLogging(signalR.LogLevel.None)
    

    Then manage error in onclose callbacks, like below.

    connection.onclose(error => {
        //...
    
        console.log("Connection Disconnected");
    });