How it works¶
SPADE PubSub implements the publish-subscribe pattern with the XMPP Publish-Subscribe extension (XEP-0060). Instead of sending messages to each other, agents publish items in nodes of a PubSub service, and the service sends each new item to the agents subscribed to the node. Publishers and subscribers do not need to know each other: many agents can publish in a node, and many agents can receive its items.
Concepts¶
- PubSub service
- An XMPP service, provided by the XMPP server, that keeps the nodes. Its address is a JID like
pubsub.localhost(thetarget_jidargument of every operation). - Node
- A named topic, like
newsorsensors/temperature, created by an agent: its owner. A node keeps the items published in it (how many depends on the service and on the node configuration). - Item
- A piece of information published in a node: an ID and a payload, which is an XML element. A text is sent inside a
<payload xmlns="spade.pubsub"/>element. - Subscription
- An agent subscribed to a node receives a notification each time an item is published in it, or retracted from it.
Roles¶
The operations of agent.pubsub are grouped by the role of the agent in a node. An agent can have several roles:
the owner of a node can also publish in it, and subscribe to it.
| Role | Operations |
|---|---|
| Owner | create(), delete(), purge(), get_node_subscriptions(), get_nodes(), get_items() |
| Publisher | publish(), retract(), notify() |
| Subscriber | subscribe(), unsubscribe(), set_on_item_published(), set_on_item_retracted() |
Architecture¶
PubSubMixin adds a pubsub attribute (a PubSubComponent) to your agent when it connects to the XMPP server. The
component uses the XEP-0060 plugin of slixmpp, the XMPP library of SPADE, to talk
to the PubSub service of the server.
flowchart LR
subgraph agent["Your agent"]
A["MyAgent(PubSubMixin, Agent)"] --> C["agent.pubsub<br>(PubSubComponent)"]
B["setup() and behaviours"] -- call --> C
end
C -- uses --> X["slixmpp<br>XEP-0060 plugin"]
X <-- XMPP --> S[("XMPP server<br>PubSub service")]
All the operations are coroutines: await them from setup() or from a behaviour. They do not raise exceptions when
the service answers with an error: see Errors.