Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a background task with SignalR

Tags:

signalr

I am using Microsoft SignalR in order to push notification to browsers. Those notifications are triggered by action from other browsers. I want to make a background task which send notification sometimes. For example, at 12:45:21 i want to fire a notification to all connected users, even if they are doing nothing. Is it possible to do that ?

like image 543
Bob5421 Avatar asked Sep 08 '16 11:09

Bob5421


2 Answers

SignalR doesn't give you the ability to run a background task, but if you run are running a background task, there is nothing to stop your task using your SignalR hub to invoke client methods and send any desired notification.

To launch and control your background task, Hangfire is a flexible library that should help.

Edit to add: Since you've clarified you want to do this in a windows service, another prominent library to assist with building and deploying services is TopShelf

Edit to add: Also, I gather from your comment that you're trying to understand how to access the hub object from your background task? There are many ways to do this, but to improve testability and maintainability of your program, I recommend using an IoC (Inversion of Control) container, and injecting the necessary references - this tutorial: Dependency Injection in SignalR has a walkthrough using the Ninject library. That walkthrough is oriented towards asp.net hosting, but the link you found should help with adapting to a windows service.

like image 158
Chris Simon Avatar answered Oct 13 '22 07:10

Chris Simon


If you are using asp.net core 2.1, this is now possible using BackgroundService/IHostedService

https://github.com/davidfowl/UT3/blob/fb12e182d42d2a5a902c1979ea0e91b66fe60607/UTT/Scavenger.cs#L9-L40 (Contents below)

public class Scavenger : BackgroundService
    {
        private readonly IHubContext<UTTHub> _hubContext;
        private readonly ILogger<Scavenger> _logger;
        public Scavenger(IHubContext<UTTHub> hubContext, ILogger<Scavenger> logger)
        {
            _hubContext = hubContext;
            _logger = logger;
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                // Mark games that haven't played in a while as completed
                var changed = Game.MarkExpiredGames();

                // Mark completed games as removed
                var removed = Game.RemoveCompletedGames();

                if (removed > 0)
                {
                    _logger.LogInformation("Removed {GameCount} games.", removed);
                }

                if (removed > 0 || changed)
                {
                    await _hubContext.Clients.All.SendAsync("GameUpdated", Game.GetGames());
                }

                await Task.Delay(5000);
            }
        }
    }
}

Also see this https://github.com/aspnet/Docs/issues/8925

like image 26
Gopal Krishnan Avatar answered Oct 13 '22 06:10

Gopal Krishnan