Join multi-user chat with node-xmpp-client

XEP-0045 defines the XMPP protocol extensions required to join multi-user chat rooms, and receive and post messages. In this post, I use the node-xmpp-client library to join a multi-user chat room. Messages sent to the chat room are received but are not parsed.

node-xmpp-client can be installed using npm, as follows

npm i node-xmpp-client

Here’s a script that creates a new XMPP client, and sends a presence stanza to the XMPP server, to join a chat room

var Client = require("node-xmpp-client")

var jid = "user@domain"
var password = "password"
var room = "room@domain/nickname"

var client = new Client({
    jid: jid,
    password: password,
    preferred: "PLAIN"

client.on("online", function() {
    console.log("joining chat room")
    var stanza = new Client.Stanza("presence", {from: jid, to: room})

client.on("stanza", function(stanza) {
    console.log("Incoming stanza: ", stanza.toString())

client.on("error", function(e) {

process.on("SIGINT", function(code) {
    console.log("leaving chat room")
    var stanza = new Client.Stanza("presence", {from: jid, to: room, type: "unavailable"})

    setTimeout(function() {
    }, 1000);

If you don’t specify a unique nickname, the XMPP server will likely reject the request to join the room. Modify the above script appropriately, save, and invoke node to execute it

node muc.js

If you get any errors, try setting the DEBUG environment variable as follows, and try again

export DEBUG=*

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s