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...
Strophe.js MUC: creating a room and joining more than one room
6.9k Views Asked by Hampel Előd At
2
There are 2 best solutions below
0

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.
A room config is like (you'll get a form on the first step of available fields if you check the response from the server).
The 2nd step looks like: