2
votes

I've created small module for website, which uses SignalR to detect if user needs to refresh the browser. Locally it works, but when code went into production, thousands of errors are produced:

Server side: (mainProject/Hubs directory):

public class AppVersionNotifierHub : Hub<IAppVersionNotifierHub>
{
    public void CheckAppVersion(string version)
    {
        // if client has diffrent version, invoke callback
        if (Global.Version != version)
        {
            Clients.Caller.NewAppVersion(Global.Version);
        }
    }
}

Javascript (type script):

this.subscribeVersionChecker = () => {
    var hub = (<any>$.connection).appVersionNotifierHub;

    hub.client.newAppVersion = (version: string) => {
        .. some logic
    }

    $.connection.hub.start(() => {
            hub.server.checkAppVersion(customerVersion.text());
    });

    $.connection.hub.reconnected(() => {
        setTimeout(() => {
            hub.server.checkAppVersion(customerVersion.text());
        }, 5000); // Restart connection after 5 seconds.
    });

    $.connection.hub.disconnected(() => {
        setTimeout(() => {
            $.connection.hub.start();
        }, 10000); // Restart connection after 10 seconds.
    });
};

Any ideas why some clients generates errors ?

  • Site is hosted on azure
  • To use bundles, I've copied dynamically generated signalr.js file into Scripts\signalrhub.js file
1
@blas3nik I don't think so, since only a few clients have problems. - Thiago Custodio
@tomasz.salieri it might be the browser version (a mobile one for example) - Thiago Custodio

1 Answers

7
votes

I have discovered the problem now. Problem was with authentication, I've read this article: http://www.bitwisejourneys.com/signalr-authenticating-even-when-you-dont-think-it-is/ and after some thinking I was able to reproduce the problem.

  1. Open site in one tab and log in
  2. Open site in the other tab and log out
  3. Restart the connection on server
  4. Tab 1 is sending the same authentication token as Tab 2. Server denies Tab 1 but responds to Tab 2

My solution: when connection is lost, I'm not trying to reconnect, but to stop and restart the connection (note I've changed the event from Reconnected to Reconnecting !)

    $.connection.hub.reconnecting(() => {
          $.connection.hub.stop();
          setTimeout(() => {
             $.connection.hub.start();
             }, 5000); // Restart connection after 5 seconds.
    });