In the situation where SignalR is being utilized, one option is to broadcast the current list of users to each connection and update the view, similar to how it's done in the sample chat application.
If regular ajax server polling is being used instead, a client method can be set on an interval to retrieve a list of current page users from a database store. Due to http's lack of state, users who have not posted recently would need to be removed from the list.
Client code:
setInterval(checkServer,5000);
function checkServer(){
//ajax call to the server here
//to fetch the user list
...
success:function(result){
$('#usersInRoom').val(result);
}
}
Server code (assuming all current page users are stored in a table with a LastModified field):
CurrentPageUser class (saved in DbContext as CurrentPageUsers table):
public class CurrentPageUser{
public int Id {get;set;}
public DateTime LastModified {get;set;}
public string Name {get;set;}
}
Controller method:
public string GetCurrentUsers(int selfid){
using (var db=new DbContext()){
var now=DateTime.Now;
//update requesting user so others can see them
db.CurrentPageUsers.Find(selfid).LastModified=now;
db.SaveChanges();
DateTime timelimit = now.AddMinutes-5);
return string.Join(", ",
db.CurrentPageUsers.Where(u=>
u.LastModified<timelimit).Select(u=>u.Name).ToList());
}
}
It's important to note that while the SignalR implementation offers better performance and is simpler to implement on the server side without needing a database table for storing current page users, it was specifically designed for this type of scenario.