Base solution for your next web application
Open Closed

abp.signalr.autoconnect is false by default #6762


User avatar
0
JapNolt created

I noticed in the default project that abp.signalr.autoconnect is set to false in ngAfterViewInit() of app.component.ts.

I want to change this for my app to autoconnect, but just wanted to make sure that there isn't an important reason for setting it to false before making that change.

Are there any gotchas to doing this?


13 Answer(s)
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, the changes were introduced in v5.6 to start the connection in chat service instead.

    See https://github.com/aspnetzero/aspnet-zero-core/commit/e535576934c3b10a85be8ae8d19ad46d5b229bbb

    There were some issues regarding angular change detection here.

    https://github.com/aspnetzero/aspnet-zero-core/issues/1333

  • User Avatar
    0
    alexanderpilhar created

    I'm curious about this as well.

    I have simulated a bad connection using clumsy and my SignalR client (custom; but based on chat-service) doesn't reconnect once the connection is lost because abp.signalr.autoConnect is set to false.

    Also, I cannot find any piece of code that sets abp.signalr.autoConnect to true for chat-service after it has been set to false ... So, I guess, chat-service also does not reconnect once the connection is lost.

    EDIT When I set abp.signalr.autoConnect to true SignalR clients try to reconnect as expected. Also, if connection is really bad for a little longer time, the SignalR client might crash because of being unable to complete negotiation with the server, not being able to do anything afterwards (no reconnecting anymore) - see:

  • User Avatar
    0
    ismcagdas created
    Support Team

    @japnolt, @ryancyq is right. If SignalR connects automatically in the angular zone, it causes so many change detections and it creates a performace issue.

    Moving SignalR Connection to out of Angular zone solves this problem.

    @alexanderpilhar does setting abp.signalr.autoConnect to true in here https://github.com/aspnetzero/aspnet-zero-core/blob/dev/angular/src/app/shared/layout/chat/chat-signalr.service.ts#L119 solves reconnection problem ?

  • User Avatar
    0
    alexanderpilhar created

    @ismcagdas I guess so - but also just not setting it to false in AppComponent.ngAfterViewInit() does the trick. Which leads me to the question why you guys set it to false there? Does it have to do with angular zone and change detection as well? I think I just don't fully understand the abp.signalr-object's lifecycle here.

  • User Avatar
    0
    JapNolt created

    @ismcagdas It seems to me the autoconnect tries to connect one time when hitting the onclose event, but if it fails to connect then it doens't try to connect again.

    Is there some recommended way to do this?

    Also I noticed in the chat-signalr.service init method it calls abp.signalr.connect(); and then abp.signalr.startConnection. Could you explain a little what the need for both of them are, or how they work?

  • User Avatar
    0
    ismcagdas created
    Support Team

    @alexanderpilhar

    Does it have to do with angular zone and change detection as well?

    Yes.

    @japnolt

    If autoconnect is set to true SignalR should try to connect in every 5 seconds, see https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web.Resources/Abp/Framework/scripts/libs/abp.signalr-client.js#L31

    There are two hubs on the server side, AbpCommonHub and ChatHub, connect function connects to common hub and startConnection connects to chat hub. common hub is used for notification communication.

  • User Avatar
    0
    JapNolt created

    @ismcagdas Wouldn't setInterval be needed to check every 5 seconds instead of setTimeout?

  • User Avatar
    0
    JapNolt created

    Actually, I believe that the onClose event only fires if the connection actually successfully connected. So if the initial connection was successful but then closed, you will try to open the connection once but then never try again.

    See better reconnection logic here from David Fowler: https://github.com/davidfowl/UT3/blob/dac409886c1bb7aec7c150b74d4ce9a3e246f03c/UTT/wwwroot/js/utt.js#L141-L153

  • User Avatar
    0
    alexanderpilhar created

    @japnolt

    Wouldn't setInterval be needed to check every 5 seconds instead of setTimeout?

    No. If the connection is closed onclose() event is fired once. In this event we re-start() the connection after waiting for a 5 seconds timeout. If this fails onlose() is fired once again. So, this is a kind of loop already.

    See better reconnection logic here from David Fowler

    This seems to be more robust indeed! Because it doesn't rely on the onclose() event to be fired but restarts on any error catched.

  • User Avatar
    0
    JapNolt created

    @alexanderpilhar @ismcagdas It seems to me that the onclose() only fires once and not again when connection.start() fails. This is using SignalR Core, is it supposed to fire onclose() again in signalr core?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I will test it again and let you know.

  • User Avatar
    0
    alexanderpilhar created

    @japnolt on my side it works as expected - but abp.signalr.autoConnect needs to be set to true somewhere. If set to false, onclose() will fire only once when connection fails for the first time (either it fails at start or it fails at some later point) and will not try to reconnect (and therefor onclose() won't fire another time).

    Also, notice that the SignalR JavaScript client does not automatically try to reconnect by itself: ASP.NET Core SignalR JavaScript client - Reconnect clients

  • User Avatar
    2
    aaron created
    Support Team

    Thanks @japnolt @alexanderpilhar @ryancyq @ismcagdas for the discussion.

    I have verified the problems, tested the solutions and submitted these PRs: