Search code examples
asp.net-corewebsocketsignalrsignalr.clientasp.net-core-signalr

.Net Core SignalR server to server reconnect


I am trying to learn more about reconnecting a websocket connection server to server when my web socket service goes down. I have done a good bit of looking around docs and other problems (mostly finding client to server), but can't decide what I am looking to implement.

So the goal is I have micro service A connecting to a web socket on micro service B when service A starts. Everything works well, but when I take down service B, then the HubConnection state is always disconnected in Service A when I boot B back up. So for example using Microsoft.AspNetCore.SignalR.Client 1.1.0

    public class MessageHubProxy : IMessageHubProxy 
    {
        private readonly HubConnection _hubConnection;

        public MessageHubProxy()
        {
            _hubConnection = new HubConnectionBuilder().WithUrl("http://localhost:54994/messageHub").Build();

            InitiateConenction().Wait();
        }

        private async Task InitiateConenction()
        {
            await _hubConnection.StartAsync();
        }

        public void AddMessage(string message)
        {
            _hubConnection.InvokeAsync("addMessage", post);
        }
    }

when I look at _hubConnection in AddMessage after I stop and start service B and call AddMessage.. I am seeing the following properties on the HubConnection:

HandshakeTimeout: 15 seconds,
KeepAliveInterval: 15 seconds,
ServerTimeout: 30 seconds,
State: Disconnected

From what I read, maybe I am still misunderstanding the use of HandshakeTimeout, KeepAliveInterval and ServerTimeout.. maybe I can use one of these to have service A reconnect once service B is up and running? Or I see the HubConnection type has a .On<>, maybe I need to catch Disconnect and call retry manually? Any information how I can handle server to server reconnects in this sense is greatly appreciated!


Solution

  • You can use Polly, when your connection fails. https://github.com/App-vNext/Polly

    public class MessageHubProxy : IMessageHubProxy 
    {
        private readonly HubConnection _hubConnection;
    
        public MessageHubProxy()
        {
            _hubConnection = new HubConnectionBuilder().WithUrl("http://localhost:54994/messageHub").Build();
    
             Policy
            .Handle<Exception>()
            .WaitAndRetry(5, r => TimeSpan.FromSeconds(5), (ex, ts) => { Log.Error("Error connecting to DB. Retrying in 5 sec."); })
            .Execute(() => InitiateConenction());
        }
    
        private async Task InitiateConenction()
        {
            await _hubConnection.StartAsync();
        }
    
        public void AddMessage(string message)
        {
            _hubConnection.InvokeAsync("addMessage", post);
        }
    }