Search code examples
javaservletsdwr

How to update users list using DWR


I am developing chat client which can connect to Gtalk and Facebook.I am using DWR for the purpose.

Once I log into I have to populate the user s lists. On client side I have

function showUsersOnline() {
    var cellFuncs = [ function(user) {

        return '<i>'+user+'</i>';
    } ];
    LoginG.usersOnline( {
        callback : function(users) {
            dwr.util.removeAllRows('usersOnline');
            dwr.util.addRows("usersOnline", users, cellFuncs, {
                escapeHtml : false
            });

On server side I am using Smack Api to get the roster list(online)

public void usersOnline() {
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
    System.out.println(roster.getEntryCount());
    int count1 = 0;
    int count2 = 0;
    for (RosterEntry r : entries) {
        String user = r.getUser();
        Presence presence = roster.getPresence(user);
        if (presence.getType() == Presence.Type.available) {
            System.out.println(user + " is online");
            count1++;

        } else {
            System.out.println(user + " is offline");
            count2++;
        }

Now should I return the data as JSON or is there a way DWR can handle the collection???


Solution

  • If you modify your server method usersOnline() to return the Collection<RosterEntry> object then DWR will populate that in the argument of the callback function which in your case is function(users). So after the call is returned back to the callback function function(users) you can go through the users object to get the updates made to it by the server side method. The users object will need to be traversed like an array since you are returning a Collection or a List whatever applies.

    Is this what you are looking for? More on this can be read here.