Publish-Subscribe pattern and PubSub in ejabberd

Publish–Subscribe is a messaging pattern where senders of messages, called publishers, do not send the messages directly to specific receivers, called subscribers. Instead, publishers categorize messages into channels without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more channels and only receive messages that are of interest, without knowledge of which publishers, if any, there are.

» Don’t want to configure PubSub yourself?
ProcessOne experts will make your business instantly connected. Contact us »

Publish-Subscribe pattern and PubSub in ejabberd

ejabberd and PubSub

In case of ejabberd, the Publish-Subscribe pattern (PubSub) is implemented by the native module mod_pubsub. From the outside, it works very similar to ejabberd MQTT module mod_mqtt. However, mod_pubsub uses XML and iq stanzas for communication. Therefore, ejabberd PubSub brings all the advantages as well as complexities that come with XML.

Create a PubSub node

Before we start, make sure you are using an XMPP client that has an option of sending raw XML input to your server. One of such clients is Psi, available for many platforms.

Next, we need to keep in mind that in ejabberd mod_pubsub is enabled by default with plugins flat and pep. The permission to create nodes is limited to local accounts.

To create your first node, send the following command using the “XML Input” within the Psi XML Console. Substitute in to and node params with your server domain, and mf in the /home/ with your username. The node in this example is called open.

<iq type="set" to="" id="create1">
  <pubsub xmlns="">
    <create node="/home/"/>

To verify that the node was created, you can send the above command again, at which point you should get a 409 error saying “Node already exists”. To create more nodes, remember to increment or change the id value of the iq element, like create1, create2 etc. and change the node’s name in the /home/ path.

Please note that the correct node path: /home/[domain]/[username]/[nodename] is important. With the default mod_pubsub permissions, the plugin will allow you to create this node. If you would like to create your node in another path, refer to the docs.

Subscribe the PubSub client

Now we can subscribe our client to receive messages from the node we just created. To do this, we use the following command, where we subscribe the specified jid to the specified node:

<iq type="set" to="" id="sub1">
  <pubsub xmlns="">
    <subscribe node="/home/"

If successful, you should see in your XML terminal a response containing a section like this:

<subscription subid="65026C52DADF1" node="/home/" jid="" subscription="subscribed"/>

Publish with PubSub client

Now we can test our new PubSub communication channel by sending the first message to the node we created. To do this, we use the following command:

<iq type="set" to="" id="publish1">
  <pubsub xmlns="">
    <publish node="/home/">
        <entry xmlns="">
          <title>Hello Brave New World</title>
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them?

In response, you should see the message relied back to you, as well as an acknowledgement looking like the following:

<iq to="" from="" type="result" id="publish1">
  <pubsub xmlns="">
    <publish node="/home/">
      <item id="650270397BE77"/>

When publishing, the item entry can contain more tags with things like dates, links etc. Please refer to this post for more examples.

What can we do with ejabberd PubSub?

If you look at the ejabberd PubSub features you can see that it contains a vast amount of options at your disposal, like fine-grained authorization, subscription monitoring and management, message retractions and more.

When you compare it to the earlier tutorial on ejabberd MQTT broker, you can see a much wider range of features available out-of-the-box when using PubSub.

In this ejabberd tutorial series:

Photo by Sawyer Bengtson on Unsplash

Let us know what you think 💬

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.