> ## Content Index
> Fetch the complete content index at: https://www.process-one.net/llms.txt
> Use this file to discover other available public pages before exploring further.

# Publish-Subscribe pattern and PubSub in ejabberd
- URL: https://www.process-one.net/blog/publish-subscribe-pattern-and-pubsub-in-ejabberd/
- Published: 2021-03-19T15:21:00.000Z
- Updated: 2024-09-16T14:21:55.000Z
- Author: ProcessOne

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 »](https://www.process-one.net/en/company/contact)

# 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](https://www.process-one.net/blog/ejabberd%5Fpubsub%5Ffeatures/) 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](https://github.com/psi-im/psi?ref=process-one.net), 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`.

```xml
<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 `create1`, `create2` 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](https://docs.ejabberd.im/archive/20%5F04/modules/?ref=process-one.net#mod-pubsub).

# 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`:

```xml
<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:

```xml
<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:

```xml
<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:

```xml
<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](https://www.ejabberd.im/mod%5Fpubsub-usage/index.html?ref=process-one.net) for more examples.

# What can we do with ejabberd PubSub?

If you look at the [ejabberd PubSub features](https://www.process-one.net/blog/ejabberd%5Fpubsub%5Ffeatures/) 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](https://www.process-one.net/blog/starting-with-mqtt-protocol-and-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:

- [How to move the office to ejabberd XMPP server](https://www.process-one.net/blog/how-to-move-the-office-to-real-time-im-on-ejabberd/)
- [How to set up ejabberd video & voice calling (STUN/TURN)](https://www.process-one.net/blog/how-to-set-up-ejabberd-video-voice-calling/)
- [How to configure ejabberd to get 100% in XMPP compliance test](https://www.process-one.net/blog/how-to-configure-ejabberd-to-get-100-in-xmpp-compliance-test/)
- [Check ejabberd XMPP server useful configuration steps](https://www.process-one.net/blog/ejabberd-xmpp-server-useful-configuration-steps/)
- [Starting with MQTT protocol and ejabberd MQTT broker](https://www.process-one.net/blog/starting-with-mqtt-protocol-and-ejabberd-mqtt-broker/)
- [Getting started with WebSocket API in ejabberd](https://www.process-one.net/blog/getting-started-with-websocket-api-in-ejabberd/)
- [Install and configure MariaDB with ejabberd](https://www.process-one.net/blog/install-and-configure-mariadb-with-ejabberd/)
- [Publish-Subscribe pattern and PubSub in ejabberd](https://www.process-one.net/blog/publish-subscribe-pattern-and-pubsub-in-ejabberd/)

*Photo by* [*Sawyer Bengtson*](https://unsplash.com/photos/YaSH0DbU5lE?ref=process-one.net) *on Unsplash*