Question

I want to update a table in a database to know which user is online after a specfic interval .Is there a way to use ajax or Jquery to make call to a controller after a specified interval? ***Please note as I have already mentioned MVC(ASP.NET) ,I need to specify that I am not using default ASP.NET membership provider. If I had used default membership I could have used method such as IsUserOnline() and got the result. Thanks

Was it helpful?

Solution 2

I have found out the solution to the problem after some research. We will use the session to make sure that database calls are not made again and again. In the cshtml file ,this is the required Javascript code.

   <script>
    (function worker() {
      $.ajax({
        url : '/test/OnlineAt',
        success: function(data) {
          $('.text').html(data);
        },
        complete: function() {
          setTimeout(worker, 30000);
        }
      });
    })();
   </script>

Now as per the question code above will send the request to the controller(test) action(OnlineAt) at the page load and after every 30 secs(30000 ms) .

Now in the controller action this is the code

      public bool OnlineAt()
    {
        int CurrentUserID = db.UserProfileTables.Where(a=> a.UserName==User.Identity.Name).FirstOrDefault().UserID;
        try
        {

           string Date = Session.Contents["OnlineAt"].ToString();

        }
        catch (Exception E)
        {
            //Add value to session
            Session.Add("OnlineAt", DateTime.Now);
            var OnlineAtData = db.OnlineTables.Find(CurrentUserID);
            var temp = OnlineAtData;
            //If value exists 
            //Update it

            if (temp!=null)
            {
                //Code to update the existing record
                OnlineAtData.OnlineAt = DateTime.Now;
                db.OnlineTables.Attach(OnlineAtData);
                var entry = db.Entry(OnlineAtData);
                entry.Property(a => a.OnlineAt).IsModified = true;
                db.SaveChanges();
                //Code to update the row
            }


            //Else Create a new record in the table
            else
            {
                OnlineTable OT = new OnlineTable();
                OT.OnlineAt = DateTime.Now;
                OT.UserID = CurrentUserID;
                db.OnlineTables.Add(OT);
                db.SaveChanges();
                //Create a new Row
            }

        }
            return true;
    }

Now that we have the Users currently online ,we just need to retrieve the values.

I consider that someone online if he has updated his record in the table in last 20 minutes .For that we can write this code in any controller where we require this value.

     DateTime RequiredTime = new DateTime();
        RequiredTime = DateTime.Now.AddMinutes(-20.0);
        List<int> IDs = new List<int>();

        var OnlineIDs = db.OnlineTables.Where(a => a.OnlineAt >= RequiredTime).Select(a=> a.UserID).Distinct().Take(10).ToList();

        var OnlineUsers = db.UserProfileTables.Where(a => OnlineIDs.Contains(a.UserID___)).ToLookup(a => a.UserID___);

OTHER TIPS

have a look with this library SignalR, I haven't used it, but I know it can help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top