Publish-Subscribe pattern and PubSub in ejabberd

ProcessOne
· 4 min read
Send by email

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 »

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 marekfoss.org in to and node params with your server domain, and mf in the /home/marekfoss.org/mf/open with your username. The node in this example is called open.

<iq type="set" to="pubsub.marekfoss.org" id="create1">
  <pubsub xmlns="http://jabber.org/protocol/pubsub">
    <create node="/home/marekfoss.org/mf/open"/>
    <configure/>
  </pubsub>
</iq>

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 create1create2 etc. and change the node’s name in the /home/marekfoss.org/mf/... 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="pubsub.marekfoss.org" id="sub1">
  <pubsub xmlns="http://jabber.org/protocol/pubsub">
    <subscribe node="/home/marekfoss.org/mf/open"
               jid="mf@marekfoss.org"/>
  </pubsub>
</iq>

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

<subscription subid="65026C52DADF1" node="/home/marekfoss.org/mf/open" jid="mf@marekfoss.org" 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="pubsub.marekfoss.org" id="publish1">
  <pubsub xmlns="http://jabber.org/protocol/pubsub">
    <publish node="/home/marekfoss.org/mf/open">
      <item>
        <entry xmlns="http://www.w3.org/2005/Atom">
          <title>Hello Brave New World</title>
          <summary>
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?
          </summary>
        </entry>
      </item>
    </publish>
  </pubsub>
</iq>

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

<iq to="mf@marekfoss.org" from="pubsub.marekfoss.org" type="result" id="publish1">
  <pubsub xmlns="http://jabber.org/protocol/pubsub">
    <publish node="/home/marekfoss.org/mf/open">
      <item id="650270397BE77"/>
    </publish>
  </pubsub>
</iq>

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