Wolfeatspotatoes
Wolfeatspotatoes

Reputation: 135

ASP.NET Suitable way to check for database changes

I have a MySQL Database and ASP.NET Web Application project which are connected using Entity Framework 6 database-first approach. The project is a CRUD App and using web api. This is the get method.

[HttpGet]
public IHttpActionResult getProducts()
{
    try
    {
        var prod = database.products.Select(x => new
        {
            product_id = x.product_id,
            name = x.name,
            image = x.image,
            original_price = x.original_price,
            original_GST = x.original_GST,
            photo_product = x.photo_product,
            visibility = x.visibility,
            quantity_constraint = x.quantity_constraint,
            description = x.description,
            pwp_price = x.pwp_price,
            pwp_GST = x.pwp_GST,
            updated_by = x.staff.name,
            updated_at = x.updated_at
        }
        ).ToList();
        return Ok(prod);
    }
    catch (Exception e)
    {
        return BadRequest(e.ToString());
    }
}

So in the javascript, when the page is loaded, i will call a function which uses ajax call to get the product list. The thing is the product is inserted by other project, so i want to check if any product is inserted into the database and then the object will be returned to the view page. Through googling, I found out many approaches such as using SQL Dependency, SignalR, Broker? and I am not sure which to choose and learn, since I am quite new to all of this. Is there a simpler way to do it? Maybe make ajax call every 5 seconds but only retrieve the inserted/ updated record?

Upvotes: 0

Views: 352

Answers (1)

Always_a_learner
Always_a_learner

Reputation: 1304

Call JS function on setinterval, it will be start triggering for each 5 seconds.

 window.setInterval(function(){
      /// call your function here
    }, 5000);

To stop the loop you can use

clearInterval() 

Upvotes: 1

Related Questions