Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strophe.js MUC: creating a room and joining more than one room

I'm creating a chat website and I'm using Strophe.js and the Strophe.muc.js plugin. The single chat functionalities work fine, but I also wan't to implement a group chat function where users can create rooms and invite other users to their room. Using the muc plugin, I can create a room, but the problem is that until I don't configure it (I guess), other users can't join and the room isn't persistent. I know that the muc plugin has configuration methods, but I don't know how to create the config Form object, I have no idea how should it look. This would be my first problem. Second: Is it possible that I join more then one room and get messages from all the rooms I'm in? If not, then there's no need to give me an answer to my first question...

like image 362
Hampel Előd Avatar asked Dec 12 '22 09:12

Hampel Előd


1 Answers

After trying Mark S' solution, I found I have to send presence first for creating the room. I list the whole code below and hope this helps.

//before executing the code below, you need to connect to IM server (var conn is Strophe.Connection)
var userName = "steve",
    serverName = "example.com",
    userJid = userName + '@' + serverName,
    roomJid = 'testRoom' + '@conference.' + serverName,
    iq;

//send presence first for creating room
var d = $pres({'from': userJid, 'to': roomJid + '/' + userName})
conn.send(d.tree());

iq = $iq({
    to: roomJid,
    type: 'set'
}).c("query", {
    xmlns: Strophe.NS.MUC_OWNER
});
iq.c("x", {
    xmlns: "jabber:x:data",
    type: "submit"
});

//send configuration you want
iq.c('field', { 'var': 'FORM_TYPE' }).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
iq.c('field', { 'var': 'muc#roomconfig_publicroom' }).c('value').t('1').up().up();

conn.sendIQ(iq.tree(), function () { console.log('success'); }, function (err) { console.log('error', err); });

I found if I don't send any configuration, the Instant Message server which is openfire only writes the room to cache, not database, so the room will disappear after restarting Instant Message server.

like image 152
gunblues Avatar answered Feb 09 '23 00:02

gunblues