public void SendNotification()
        {
            var name = Context.User.Identity.Name;

            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            //
            // Azure table storage repositories
            //
            var connRepository = new ConnectionsRepository();
            var notifyRepo     = new NotificationsRepository();

            if (!notifyRepo.Init())
            {
                return;
            }
            if (!connRepository.Init())
            {
                return;
            }

            var connections   = connRepository.GetConnectionsByUserId(name).ToList();
            var notifications = notifyRepo.GetNotificationsByUserId(name).ToList();

            notifications.Sort();

            if (connections.Count > 0 && notifications.Count > 0)
            {
                // Send notification(s) to each user connection.
                foreach (var connection in connections)
                {
                    foreach (var notification in notifications)
                    {
                        string json = System.Web.Helpers.Json.Encode(new
                        {
                            title          = notification.Title,
                            message        = notification.Message,
                            url            = string.IsNullOrEmpty(notification.Url) ? string.Empty : notification.Url,
                            linkText       = string.IsNullOrEmpty(notification.LinkText) ? string.Empty : notification.LinkText,
                            type           = (int)notification.NotifyTypeInt,
                            notificationId = notification.RowKey
                        });

                        // Calls method on client side to show notification
                        Clients.Client(connection.RowKey).addNotification(json);
                    }
                }
            }
        }
        public override Task OnDisconnected(bool stopCalled)
        {
            string name = Context.User.Identity.Name;

            var repository = new ConnectionsRepository();

            if (repository.Init())
            {
                RemoveConnection(name, Context.ConnectionId, repository);
            }

            return(base.OnDisconnected(stopCalled));
        }
        public void RemoveConnection(string name, string connectionId, ConnectionsRepository repo)
        {
            if (!repo.Init())
            {
                return;
            }

            if (!string.IsNullOrEmpty(name))
            {
                repo.RemoveConnection(name, connectionId);

                // Just in case user has old connections in table
                RemoveOldUserConnections(name, 1, repo);

                // TODO testing only
                //var connections = repo.GetAllConnections();
            }
        }
        public override Task OnConnected()
        {
            var name = Context.User.Identity.Name;

            if (!string.IsNullOrEmpty(name))
            {
                var repository = new ConnectionsRepository();
                if (repository.Init())
                {
                    var connection = new ConnectionModel(name, Context.ConnectionId, DateTime.Now);

                    repository.AddConnection(connection);

                    // TODO testing only
                    //var connections = repository.GetAllConnections();
                }
            }

            return(base.OnConnected());
        }
        public void RemoveOldUserConnections(string who, int daysOld, ConnectionsRepository repo)
        {
            if (!repo.Init())
            {
                return;
            }

            var connections = repo.GetConnectionsByUserId(who).ToList();

            var timeSpan = TimeSpan.FromDays(daysOld);

            var dateTimeOffset = DateTime.Now.Subtract(timeSpan);

            foreach (var connection in connections)
            {
                // Remove connection if older that 'daysOld'
                if (connection.ConnectionTimeStamp <= dateTimeOffset)
                {
                    RemoveConnection(connection.PartitionKey, connection.RowKey, repo);
                }
            }
        }