XMPP strophe.js demo code

Share This:


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:


Send message in group/room using strophe.js

function sendGroupMsg() { var room_name = $('#room').val() + '@conference.localhost'; var msg = $('#msg').val(); Chat.sendMessage(room_name, msg, "groupchat"); }


var Chat = { sendMessage: function (messgeTo, message, type) { var messagetype = (type) ? type : 'chat'; var reply; if (messagetype === 'groupchat') { reply = $msg({to: messgeTo, from: Chat.connection.jid, type: messagetype, id: Chat.connection.getUniqueId() }).c("body", {xmlns: Strophe.NS.CLIENT}).t(message); } else { reply = $msg({to: messgeTo, from: Chat.connection.jid, type: messagetype }).c("body").t(message); } Chat.connection.send(reply.tree()); Chat.log('I sent ' + messgeTo + ': ' + message, reply.tree()); } }

Share This: