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)
-
0
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
-
0
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 tofalse
.Also, I cannot find any piece of code that sets
abp.signalr.autoConnect
totrue
for chat-service after it has been set tofalse
... So, I guess, chat-service also does not reconnect once the connection is lost.EDIT When I set
abp.signalr.autoConnect
totrue
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: -
0
@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
totrue
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 ? -
0
@ismcagdas I guess so - but also just not setting it to
false
inAppComponent.ngAfterViewInit()
does the trick. Which leads me to the question why you guys set it tofalse
there? Does it have to do with angular zone and change detection as well? I think I just don't fully understand theabp.signalr
-object's lifecycle here. -
0
@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 thenabp.signalr.startConnection
. Could you explain a little what the need for both of them are, or how they work? -
0
@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.
-
0
@ismcagdas Wouldn't setInterval be needed to check every 5 seconds instead of setTimeout?
-
0
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
-
0
@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 failsonlose()
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. -
0
@alexanderpilhar @ismcagdas It seems to me that the
onclose()
only fires once and not again whenconnection.start()
fails. This is using SignalR Core, is it supposed to fireonclose()
again in signalr core? -
0
Hi,
I will test it again and let you know.
-
0
@japnolt on my side it works as expected - but
abp.signalr.autoConnect
needs to be set totrue
somewhere. If set tofalse
,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 thereforonclose()
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
-
2
Thanks @japnolt @alexanderpilhar @ryancyq @ismcagdas for the discussion.
I have verified the problems, tested the solutions and submitted these PRs: