Skip to content

API Reference

The reference of the public API of SPADE PubSub, generated from its docstrings. The guides explain how to use it.

  • PubSubMixin: add it to the base classes of your agent, before Agent. It creates agent.pubsub when the agent connects to the XMPP server.
  • PubSubMixin.PubSubComponent: the class of agent.pubsub, with all the PubSub operations.

PAYLOAD_NAMESPACE module-attribute

PAYLOAD_NAMESPACE = 'spade.pubsub'

Namespace of the <payload/> element that wraps the text payloads.

PubSubMixin

This mixin provides PubSub support to SPADE agents. It must be used as superclass of a spade.Agent subclass.

PubSubComponent

PubSubComponent(client)

PubSub (XEP-0060) operations of an agent, available as agent.pubsub.

The operations are coroutines. When the server answers with an error, or does not answer in time, the error is logged and the operation returns None (or an empty list, for the operations that return lists).

create async

create(target_jid: str, target_node: str | None = None, config_form: Form | None = None) -> str | None

Create a new node at a service.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str or None

Name of the PubSub node to create (None to create an instant node, named by the service).

None
config_form Slixmpp Form

Dataform to configure the node to create.

None

Returns:

Type Description
str | None

The name of the node created, or None on error.

delete async

delete(target_jid: str, target_node: str | None)

Delete an existing node.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str or None

Name of the PubSub node to delete.

required

get_node_subscriptions async

get_node_subscriptions(target_jid: str, target_node: str | None) -> list[str]

Return the JIDs subscribed to a node (only the owner of the node can ask).

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str

Name of the node to query.

required

Returns:

Type Description
list[str]

The bare JIDs subscribed to the node (an empty list on error).

purge async

purge(target_jid: str, target_node: str | None)

Delete all items from a node.

Parameters:

Name Type Description Default
target_jid str

JID of the PubSub service

required
target_node str

Name of the PubSub node

required

get_nodes async

get_nodes(target_jid: str, target_node: str | None = None) -> list[dict]

Request all nodes at a service or collection node.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str or None

Name of the collection node to query

None

Returns:

Type Description
list[dict]

A list of dicts with the keys jid, node and name (an empty

list[dict]

list on error).

get_items async

get_items(target_jid: str, target_node: str | None) -> list[tuple[str, Element]]

Request all items of a node.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str or None

Name of the PubSub node.

required

Returns:

Type Description
list[tuple[str, Element]]

A list of tuples (id, payload), where payload is the XML element

list[tuple[str, Element]]

published (payload.text is the text of a text payload). An empty

list[tuple[str, Element]]

list if the node has no items, or on error.

subscribe async

subscribe(target_jid: str, target_node: str | None = None, subscription_jid: str | None = None, config=None) -> str | None

Subscribe to a node.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str

Name of the PubSub node to subscribe to.

None
subscription_jid str

The address to subscribe to the service.

None
config Data

Optional configuration of the subscription

None

Returns:

Type Description
str | None

The subscription ID given by the service (None if it gives none, or on

str | None

error).

unsubscribe async

unsubscribe(target_jid: str, target_node: str | None = None, subscription_jid: str | None = None, subid=None)

Unsubscribe from a node.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str

Name of the PubSub node to unsubscribe from.

None
subscription_jid str

The address to subscribe from the service.

None
subid str

Unique ID of the subscription to remove.

None

set_on_item_published

set_on_item_published(callback)

Call callback(message) when an item is published to a node the agent is subscribed to.

The item is message["pubsub_event"]["items"]["item"]: its "id" and its "payload" (the XML element published).

set_on_item_retracted

set_on_item_retracted(callback)

Call callback(message) when an item is retracted (with notify=True) from a node the agent is subscribed to.

The ID of the item is message["pubsub_event"]["items"]["retract"]["id"].

notify async

notify(target_jid: str, target_node: str)

Notify all subscribers of a node without publishing an item. “Publish” to the node at jid without any item. This merely fans out a notification.

Only transient, notification-only nodes accept publishing without an item (XEP-0060 §7.1.3.6): other nodes answer with an item-required error.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str

Name of the PubSub node to send a notify from.

required

publish async

publish(target_jid: str, target_node: str, payload: Element | str, item_id: str | None = None, ifrom: str | None = None) -> str | None

Publish an item to a node.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str

Name of the PubSub node to publish to.

required
payload Element | str

Payload to publish. A text is sent inside a <payload xmlns="spade.pubsub"/> element; an XML element is sent as it is.

required
item_id str or None

Item ID to use for the item (None to let the service give one).

None

Returns:

Type Description
str | None

The ID of the item published, or None on error.

retract async

retract(target_jid: str, target_node: str, item_id: str, notify=False)

Retract a previously published item from a node.

Parameters:

Name Type Description Default
target_jid str

Address of the PubSub service.

required
target_node str

Name of the PubSub node to send a notify from.

required
item_id str

The ID of the item to retract.

required
notify bool

Flag indicating whether subscribers shall be notified about the retraction.

False