Skip to content

Managing nodes

A node is a topic of the PubSub service where agents publish items. The agent that creates a node is its owner: it can configure it, see its subscribers, remove its items and delete it.

In the examples, PUBSUB is the address of the PubSub service (e.g. "pubsub.localhost"), and the operations are called from a behaviour (self.agent.pubsub) or from setup() (self.pubsub).

Creating a node

node = await self.agent.pubsub.create(PUBSUB, "news")

create() returns the name of the node, or None if the service refused to create it (see Errors): for example, because another agent already has a node with that name. Creating again a node that the agent already owns is not an error in the XMPP server of SPADE.

Instant nodes

XEP-0060 lets a service name the node (create(PUBSUB), without a name): create() then returns the name given by the service. Not every service supports it: the XMPP server of SPADE does not.

With a configuration

Pass a data form with the node configuration to create and configure the node in one request:

form = self.agent.client.plugin["xep_0004"].make_form(ftype="submit")
form.addField(
    "FORM_TYPE", ftype="hidden", value="http://jabber.org/protocol/pubsub#node_config"
)
form.addField("pubsub#persist_items", value=True)
form.addField("pubsub#max_items", value="10")

await self.agent.pubsub.create(PUBSUB, "news", form)

Which options a service supports, and their defaults, depend on the service.

The XMPP server of SPADE

PyJabber accepts the configuration form since version 0.5.1, but does not apply it yet: its nodes keep up to 1024 items, and only the owner publishes in them.

Listing the nodes

nodes = await self.agent.pubsub.get_nodes(PUBSUB)
# [{"jid": "pubsub.localhost", "node": "news", "name": None}, ...]

Any agent can list the nodes of a service. Each node is a dictionary with the address of the service (jid), the name of the node (node) and its human-readable name (name, None if it has none). With a collection node as second argument, get_nodes() lists the nodes of the collection.

Who is subscribed

The owner of a node can get the JIDs subscribed to it:

subscribers = await self.agent.pubsub.get_node_subscriptions(PUBSUB, "news")
# ["subscriber@localhost"]

For any other agent, the service answers with an error, and get_node_subscriptions() returns an empty list.

Removing all the items (purge)

await self.agent.pubsub.purge(PUBSUB, "news")

The node and its subscriptions are kept: only its items are removed.

Deleting a node

await self.agent.pubsub.delete(PUBSUB, "news")

Only the owner of a node can delete it. Its items and subscriptions are deleted with it.