node-xmpp --- Create a Persistant and Private Chatroom

1.7k views Asked by At

I need to create a Private and Persistent Chat room dynamically via Node that does not automatically delete itself.

I've searched the net and couldn't find much on how to do it. This is the code snippet I use to create the chatroom:

var cl = new xmpp.Client({
    jid: jabber_creds.jid,
    password: jabber_creds.password,
    host: jabber_creds.host,
    port: jabber_creds.port
});


cl.on('online', function() {
    var room_jid = jabber_creds.room_jid.replace("%s", chatRoomName);
    // join room (and request no chat history)
    cl.send(new xmpp.Element('presence', { to: room_jid }).
        c('x', { xmlns: 'http://jabber.org/protocol/muc' })
    );

    // create room
    cl.send(new xmpp.Element('iq', { to: room_jid, id: 'create', type: 'set' }).
        c('query', { xmlns: 'http://jabber.org/protocol/muc#owner' }).
        c('x', { xmlns: 'jabber:x:data',type: 'submit' })
    );
});
3

There are 3 answers

0
dbanes On

Chat room persistence is handled at the server not the client. Yes the client can request that a server hold onto a chat room but you can't actually persist it from the client. Check with the server documentation that you are using to make sure it supports this.

0
Rubycon On

Try to follow XEP-0045

http://xmpp.org/extensions/xep-0045.html#createroom

Just read The workflow for creating and configuring such rooms is as follows section

You need to do the next:

  1. Send 1st presence to a new room
  2. Server returns you a couple of messages that room was created, but you should unlock it
  3. You should create configuration form and send to the server. In this form you can set 'Persistent' type & 'Only members' type
  4. Server will return you success result if everything is OK
0
gunblues On

You can reference https://github.com/node-xmpp/node-xmpp/blob/master/examples/create_room.js and send configuration you want by XEP-0045

//create_room.js
'use strict'

var xmpp = require('../index')
    ,argv = process.argv

if (argv.length < 5) {
    console.error('Usage: node create_room.js <my-jid> <my-password> <room-name>')
    process.exit(1)
}

var cl = new xmpp.Client({ jid: argv[2],  password: argv[3] })

cl.on('online', function(data) {
    var userJid = data.jid.user + '@' + data.jid.domain,
        roomJid = argv[4] + '@conference.' + data.jid.domain,
        pres,
        iq

    console.log('Connected as ' + userJid + '/' + data.jid.resource)

    console.log('Create room - ' + argv[4])

    pres = new xmpp.Element(
        'presence',
        { from: userJid, to: roomJid + '/' + data.jid.user })
        .c('x', {'xmlns':'http://jabber.org/protocol/muc'})

    cl.send(pres.tree())

    iq = new xmpp.Element(
        'iq',
        { to: roomJid, type: 'set' })
    .c('query', { xmlns: 'http://jabber.org/protocol/muc#owner' })
    .c('x', { xmlns: "jabber:x:data", type: "submit"})
    //set room to be hidden by sending configuration. ref: http://xmpp.org/extensions/xep-0045.html
    iq.c('field', { 'var': 'FORM_TYPE' })
    .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
    .c('field', { 'var': 'muc#roomconfig_publicroom'})
    .c('value').t('0').up().up()

    cl.send(iq.tree())

    // exit later for sending configuration done
    setTimeout(function() {
        cl.end()
    }, 100)

})

cl.on('error', function(e) {
    console.error(e)
    process.exit(1)
})