3

I'm using SignalR to push updates out to connected web clients. I listen to the disconnected event in order to know when I should start my reconnection logic

$.connection.hub.disconnected(function() {
    // Initiate my own reconnection logic
});

The SignalR hub is hosted in in IIS (along with my site)

[assembly: OwinStartup(typeof(Startup))]
namespace MyNamespace.SignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

Upon connection, the client calls a server method to join a group

public class MyHub : Hub
{
    public void JoinGroup(string groupName)
    {
        Groups.Add(Context.ConnectionId, groupName);
    }
}

And then I push messages to this group:

context.Clients.Group(groupName).sendMessage();

If I manually recycle the application pool in IIS, SignalR starts trying to reconnect and I eventually receive a disconnected event on the client side if it fails (after the timeout).

However, my problem is that if I manually restart the website in IIS, I do not receive any disconnected event at all, and I can't see in the logs that SignalR has detected any connection problem at all. How can I detect that I have been disconnected?

I know I should probably persist the group connections somehow, since that is saved in memory I guess. But that shouldn't affect the initial problem that the client receives no notification of the disconnection? Shouldn't the client side signalr code throw some kind of exception/event?

Joel
  • 8,502
  • 11
  • 66
  • 115

2 Answers2

3

disconnected fires first when the built in logic for reconnection have timed out. You also need to listen to the recconect event, something like i did here

https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/blob/ReconnectOnClosed/SignalR.EventAggregatorProxy.Client.JS/jquery.signalR.eventAggregator.js#L157

Anders
  • 17,306
  • 10
  • 76
  • 144
  • Ok, I will have a look at your code. But if I shut down the app pool I receive disconnected after ~30 seconds when signalrs reconnection logic has timed out. If I stop the website, I never get the event and signalrs reconnection logic never kicks in, it seems. – Joel Jun 16 '15 at 16:44
  • Dissconnect event should trigger after a few reconnect attempts, you can check the Chrome console and you will see the failed attempts – Anders Jun 16 '15 at 16:46
  • Hmm noticed that the tabs were wrong in the code i linked, the code should now be more easy to read – Anders Jun 16 '15 at 16:47
  • 1
    I enabled the SignalR logs to see what what happening. When I shut down the app pool, almost immediately i see in the logs that SignalR starts trying to reconnect. However, when I shut down the website instead, I see nothing in the logs... I've left work so I can't do any more tests for now. But the behavior seems strange. Do you have the possibility to test and confirm that you do not encounter this behavior? – Joel Jun 16 '15 at 17:15
  • I recently added the behavior to my library shown above and I tested it thoroughly. Both tried to recycle app pool and shutdown web site, both worked. But you need to implement both events like I have done in my code above – Anders Jun 16 '15 at 18:06
  • Ok, thank you. I will have a try when I'm back at work. Thanks for the help :) – Joel Jun 16 '15 at 18:54
  • Just a little follow up here. I never managed to solve this problem. If i press "restart" in IIS for the website, the client never understands that the connection is broken, and never times out, nor disconnects. – Joel Oct 08 '15 at 14:37
  • There must be something wrong with your config. Try the demo project in my project above, Does it work? – Anders Oct 09 '15 at 07:14
  • Thanks for still trying to help me :) I built and deployed your SignalR Demo project to a website IIS 8.5. I navigated to the site in two browser windows (client1 and client2). Tried sending messages between the two, worked liked it should! Then I restarted the website in the IIS (also tried to change the physical path to a copy of the same folder, that gives the same behaviour). Now I can no longer send messages between the clients (without reloading them). They do not understand that the connection has been broken, and doesn't try to reconnect. No client side error is thrown. – Joel Oct 09 '15 at 08:00
  • I have a live demo here, http://malmgrens.org/signalr/ running under win 12r2 (IIS8) and it works. Tried bith with recycle app pool and hard reset. – Anders Oct 09 '15 at 08:45
  • I just found out what my problem is. It has to do with the fact that I only do the equivalent of a restart of the website (not a hard reset or recycle of the app pool). I will add an answer shortly. – Joel Oct 09 '15 at 08:59
  • ah, sorry, the dissconnect code is in a branch https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/tree/ReconnectOnClosed – Anders Oct 09 '15 at 08:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91821/discussion-between-anders-and-joel). – Anders Oct 09 '15 at 09:01
  • @zoma.saf Didn't solve it. We recycle the app pool when we need to fix it. – Joel Jul 06 '17 at 11:21
1

So I finally found out what the problem is and how to solve it. Some background though:

At the moment we manually release new versions of our application by going to "Basic settings..." under the website in IIS and changing the "Physical Path" from C:\websites\version1 to C:\websites\version2. Apparently this gives the same behavior as doing a restart of the website in IIS (not a hard reset, not stopping the website, not recycling the app pool) and according to this: "does NOT shut the site down, it merely removes the Http.sys binding for that port". And no matter how long we wait, the connected clients never receive any kind of indication that they should reconnect.

So the solution is to recycle the application pool after each release. The "lost" clients will receive disconnected events and reconnect to the new version of the site.

Community
  • 1
  • 1
Joel
  • 8,502
  • 11
  • 66
  • 115