Hello SignalR Experts,
I have a requirement to create a live dashboard to display data chart pulled from the database.I am implementing the code as suggested in sitepoint blog (https://www.sitepoint.com/build-real-time-signalr-dashboard-angularjs/). It pulls data from server side as soon as the site starts even if no clients are listening.
Startup.cs code
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
app.MapSignalR();
//is this the right place for this code
DashboardService dashboardService = new DashboardService(10000); //10 second
Task.Factory.StartNew(async () => await dashboardService.GetDataFromDatabase());
}
}
DashboardService.cs
public class DashboardService
{
private IHubContext _hubs;
private readonly int _pollIntervalMillis;
public DashboardService(int pollIntervalInMilliSec)
{
_hubs = GlobalHost.ConnectionManager.GetHubContext<DashboardHub>();
_pollIntervalMillis = pollIntervalInMilliSec;
}
public async Task GetDataFromDatabase()
{
while (true)
{
await Task.Delay(_pollIntervalMillis);
//Data Logic to pull data from database
_hubs.Clients.All.broadcastToDashboard(broadcastObject);
}
}
}
Instead of pulling the data from database even if no clients are listening, I would like to pull a data from database only if at least one client is listening and stop pulling the data if no clients are listening. Is this possible? Any suggestions..