Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalRCore - Unable to get updated data. - Using Timer manager

I am learning how to implement signalr. I referred a tutorial LINK and created the sample application without a database connection which worked fine.

Tech - SinalRCore,Entityframework Core, Dontnet Core API, Angular 8

Without a database connection the SingalR was able to update the data that I am sending to angular. I can see the changed the data without any issues.

When I tried to fetch the data from database I am getting the data, but when I do an updated in the database, the update is not reflecting in the resultant json I am getting.

I am using an empty hub. A timer is used which will fire every 2 seconds.

This is my API Action.

namespace MyUtilities.Controllers
{

[Route("api/[controller]")]
[ApiController]
public class AlertsController : ControllerBase
{
    private IHubContext<AlertHub> _hub;
    private readonly FolderContext  dbContext;

    public AlertsController(IHubContext<AlertHub> hub,FolderContext Context )
    {
        _hub = hub;
        dbContext= Context;
    }

    public IActionResult Get()
    {
        var res = dbContext.Folder.Join(dbContext.FilterCount,
            f => f.lFolderId,
            e => e.lFolderId,
            (f, e) => new
            {
                lFolderId = f.lFolderId,
                sTileName = f.sMnemonic,
                Icon = f.sIcon,
                Count = e.lNewCount
            })
            .GroupBy(f => new { f.sTileName, f.Icon, f.lFolderId }).Select(x => new { Count = x.Sum(f => f.Count), x.Key.lFolderId, x.Key.Icon, x.Key.sTileName }).ToList();
        var timerManager = new TimerManager(() => _hub.Clients.All.SendAsync("transferalertdata", res));


        return Ok(new { Message = "Request Completed" });

    }

}
}

Is there any problem using context in the above way?

like image 937
Rohith Avatar asked Sep 16 '19 17:09

Rohith


Video Answer


1 Answers

When I tried to fetch the data from database I am getting the data, but when I do an updated in the database, the update is not reflecting in the resultant json I am getting.

var timerManager = new TimerManager(() => _hub.Clients.All.SendAsync("transferalertdata", res));

In your AlertsController, we can find that you retrieve the data only one time when it reached into Get action, and you assign it to variable res, then in TimerManager callback function, you just send retrieved/old data to all connected SignalR clients, not fetching data with updates from database again, so the connected SignalR clients just received old data even though the callback function you provided is executed every two seconds.

Besides, if you perform database query in code logic of callback function (or scheduled task), please note that it would be executed frequently (every two seconds), which may cause bad impact on your database server. So if possible, you can try to call/trigger "SignalR BroadCast" functionality only when some user performed update operation on database.

like image 173
Fei Han Avatar answered Oct 20 '22 15:10

Fei Han