public override async Task OnDisconnectedAsync(Exception exception) { // When user closes a browser tab or call stop() connection // we need to ensure to notify all users about offline state // This method is a same logic of Offline var isOffline = await _chatContext.TakeOfflineAsync(new OnlineUser { UserName = Context.UserIdentifier }); if (isOffline) { try { var allOpenningSessions = _chatContext.GetAllActiveSessions(Context.UserIdentifier); if (allOpenningSessions != null) { foreach (var session in allOpenningSessions) { // Persist it on Db session.LeaveDate = DateTime.UtcNow; await _chatSessionRepository.UpsertAsync(session.ToSession(true)); session.IsInDb = true; session.IsDirty = false; } } _chatContext.CloseAllUnlistenRooms(Context.UserIdentifier); } catch (Exception ex) { _logger.Error(ex, "There are some problems when saving chat session"); } finally { // Ensure we still send notify await Clients.Others.Offline(Context.UserIdentifier); } } await base.OnDisconnectedAsync(exception); }