1

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..

WorkInProgress
  • 393
  • 6
  • 16

1 Answers1

2

Use this answer as a reference.

And then check UserHandler.ConnectedIds.Any() as a condition before pulling dashboard data.

Community
  • 1
  • 1
Igor Lizunov
  • 539
  • 5
  • 12