Question

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...

Was it helpful?

Solution 2

  1. You can set the rooms to be persistent by default on your jabber server.
  2. Creating rooms is a 2 step process. First creating the room then configuring the room.
  3. You can join as many rooms as you like.

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:

var iq, stanza;
iq = $iq({
    to: newroomjid,
    type: "set"
}).c("query", {
    xmlns: Strophe.NS.MUC_OWNER
});
iq.c("x", {
    xmlns: "jabber:x:data",
    type: "submit"
});
iq.c('field', { 'var': 'FORM_TYPE' }).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
iq.c('field', { 'var': 'muc#roomconfig_roomname' }).c('value').t(roomName).up().up();
stanza = iq.tree();

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top