SignalR not working from Hangfire

I’m using SignalR to send messages from the server to the client. In a normal situation this works well, except when sending a message from a Hangfire backgroundjob. I’m using IHubContext to send the messages and if I check out the lifetimemanager of that, I can see that when running from Hangfire it doesn’t have any connections.

Has anyone ran into this issue before, I’m really interested in the solution!

I got it to work.

The job

 [Queue("subscription")]
        public static void SendScheduledReportSubscriptions()
        {
            List<string> roles = new List<string>() { "Administrators" };
            DateTime now = DateTime.UtcNow;
            now = now.AddSeconds(-now.Second);
            List<int> SubscriptionsToSend = UserReportSubscriptionsToSend(now);
            for (int i = 0; i < SubscriptionsToSend.Count; i++)
            {
                BackgroundJob.Enqueue(() => SendScheduledReportSubscription(SubscriptionsToSend[i]));
                UpdateLastExecutionUTC(SubscriptionsToSend[i], now);
            }
            SetSubscriptionJobsSucceededToDeleted();
            Notifications.InstantNotification(Notifications.Type.Default, Notifications.Sound.Default, roles, null, "");
        }

The Dependencies

public static void InstantNotification(Type? type, Sound? sound, List<string> roles, List<string> usernames, String message)
        {
            try
            {
                if (type == null) { type = Type.Default; }
                string color = "purple";
                switch (type)
                {
                    case Type.Danger: color = "red"; break;
                    case Type.Success: color = "green"; break;
                    case Type.Default: default: color = "purple"; break;
                }
                string audio = String.Empty;
                switch (sound)
                {
                    case Sound.Danger: audio = "danger"; break;
                    case Sound.Default: audio = "default"; break;
                    case Sound.Info: audio = "info"; break;
                    case Sound.Success: audio = "success"; break;
                    case Sound.Warning: audio = "warning"; break;
                }
                List<string> finalusernames = new List<string>();
                var hub = GlobalHost.ConnectionManager.GetHubContext<Hubs.InstantNotificationHub>();
                if (roles != null) { if (roles.Count > 0) { List<string> usersfromroles = WebApp.GetUserNamesForRoles(roles); finalusernames.AddRange(usersfromroles); } }
                if (usernames != null) { if (usernames.Count > 0) { finalusernames.AddRange(usernames); } }

                if (finalusernames.Count > 0)
                {
                    hub.Clients.Users(usernames).notificationReceiveMessage(message, color, audio);
                }
                else
                {
                    hub.Clients.All.notificationReceiveMessage(message, color, audio);
                }
            }
            catch { }
        }

I think what you are after is this:

 var hub = GlobalHost.ConnectionManager.GetHubContext<Hubs.InstantNotificationHub>();

and this:

hub.Clients.All.notificationReceiveMessage(message, color, audio);

Ignore the color and audio part… my signalR messages also make noise, and can be in different background colors. It is just stuff I pass on to the client JavaScript so that I can have different colors for danger, warnings, info and success.