How to join XMPP room in php

Here we have created the chat room in XMPP using PHP. Now I'm writing the code to show you how you can join a chat room in XMPP using PHP. I'm using ejabbered server.

 

Share This:


Send message to XMPP group in php

Here we have created the chat room in XMPP using PHP and Here we have defined how to join a chant room in XMPP.

Now I'm going to explain how to send a message into a chat room in XMPP.

 

Share This:


XMPP create room in php

If you wanted to send a message to a number of people you can do it with XMPP. Yes, Here I'm going to explain how you can create the chat room on XMPP ejabbered server in PHP.

 

Share This:


Send message to XMPP server in php

If you are creating any chat application or wanted to send real time messages or data you simplest possible solution is XMPP. Here I'm writing the for sending the messages on XMPP server in PHP. I'm using ejabbered server here. so both sender and receiver have to register themselves over the ejabbered server before sending the messages to each other. Here is the code to register the user on ejabbered XMPP server.

Here is the PHP code to send the messages.

 

Share This:


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:


Get room lists using strophe.js

function getRoomList() { Chat.mucListRooms(); }


var Chat = { mucListRooms: function () { Chat.connection.muc.listRooms( Strophe.getDomainFromJid(Chat.connection.jid), function (status) { Chat.log("List of Chat Rooms", status); }, function (status) { Chat.log("Error getting Chat Rooms", status); } ); } }

Share This:


Join or leave chat room using strophe.js

function joinGroup() { var room_name = $('#room').val() + '@conference.localhost'; var nickname = sessionStorage.getItem("user_name");; var password = sessionStorage.getItem("password");; Chat.mucJoin(room_name, nickname, password); }


var Chat { mucSessionInfo: {}, mucJoin: function (roomName, nickname, password) { var nickname = (nickname) ? nickname : Chat.getSubJID(Chat.connection.jid); Chat.connection.muc.join(roomName, nickname, Chat.messageReceived, Chat.presenceReceived, Chat.rosterReceived, password); Chat.mucSessionInfo['roomName'] = roomName; Chat.mucSessionInfo['nickname'] = nickname; }, mucLeave: function (exitMessage) { Chat.connection.muc.leave( Chat.mucSessionInfo['roomName'], Chat.mucSessionInfo['nickname'], Chat.presenceReceived, exitMessage ); }, }

Share This: