Add or remove a user from a room/group using strophe.js

addUser: function (Jid, name, groups) {
if (!Chat.userExists(Jid)) {
var groups = (groups) ? groups : '';
Chat.connection.roster.add(Jid, name, groups, function (status) {
Chat.Roster.push({'jid': Jid,
'name': name,
subscription: '' //NOTE:MIGHT BE ERROR PRONE TO NOT DECLARE SUBSCRIPTION...
});
Chat.log("User Added to roster: " + name, status, Chat.Roster);
});
Chat.log("Added user: " + Jid);
} else
Chat.log("Error adding new User");
},


//remove user from your roster
removeUser: function (Jid) {
if (Chat.userExists(Jid)) {
//Chat.connection.roster.get();
var iq = $iq({type: 'set'}).c('query', {xmlns: Strophe.NS.ROSTER}).c('item', {jid: Jid,
subscription: "remove"});
Chat.connection.sendIQ(iq, function (status) {
Chat.log("Removed: " + Jid, status);
});
for (var i = Chat.Roster.length - 1; i >= 0; i--) {
if (Chat.Roster[i].jid === Jid) {
Chat.Roster.splice(i, 1);
Chat.log(Chat.Roster);
}
}
} else
Chat.log("Error removing user");
},

Share This:

2 thoughts on “Add or remove a user from a room/group using strophe.js

Leave a Reply

Your email address will not be published. Required fields are marked *