Quick start¶
This program has two agents: a publisher, which creates the node news and publishes two headlines in it, and a
subscriber, which subscribes to the node and prints every headline it is notified of.
examples/news.py
"""A publisher agent publishes news in a node, and a subscriber agent receives them.
Run it with ``python examples/news.py``: SPADE starts its own XMPP server, whose
PubSub service is ``pubsub.localhost``.
"""
import asyncio
import spade
from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade_pubsub import PubSubMixin
PUBSUB = "pubsub.localhost" # the PubSub service of the XMPP server
NODE = "news"
class PublisherAgent(PubSubMixin, Agent):
class PublishNews(OneShotBehaviour):
async def run(self):
for headline in ["SPADE PubSub 0.4 released", "Agents love PubSub"]:
item_id = await self.agent.pubsub.publish(PUBSUB, NODE, headline)
print(f"[publisher] published {item_id}: {headline}")
await asyncio.sleep(0.5)
async def setup(self):
await self.pubsub.create(PUBSUB, NODE)
class SubscriberAgent(PubSubMixin, Agent):
def on_news(self, message):
item = message["pubsub_event"]["items"]["item"]
print(f"[subscriber] {item['id']}: {item['payload'].text}")
async def setup(self):
self.pubsub.set_on_item_published(self.on_news)
await self.pubsub.subscribe(PUBSUB, NODE)
async def main():
publisher = PublisherAgent("publisher@localhost", "secret")
subscriber = SubscriberAgent("subscriber@localhost", "secret")
await publisher.start() # it creates the node
await subscriber.start() # it subscribes to the node
behaviour = PublisherAgent.PublishNews()
publisher.add_behaviour(behaviour)
await behaviour.join()
await asyncio.sleep(1) # the last notification is on its way
await publisher.pubsub.delete(PUBSUB, NODE)
await subscriber.stop()
await publisher.stop()
if __name__ == "__main__":
spade.run(main(), embedded_xmpp_server=True)
Run it:
spade.run(main(), embedded_xmpp_server=True) starts the XMPP server of SPADE while the program runs, so you do not
need to install one: its PubSub service is pubsub.localhost. The output is similar to this (the item IDs are given
by the service, and the lines of both agents may appear in another order):
[publisher] published 9b0f0c5e-...: SPADE PubSub 0.4 released
[subscriber] 9b0f0c5e-...: SPADE PubSub 0.4 released
[publisher] published 3f6d2a11-...: Agents love PubSub
[subscriber] 3f6d2a11-...: Agents love PubSub
What happens¶
PubSubMixingives both agents thepubsubattribute, with all the PubSub operations. It is created when the agent connects to the XMPP server, so it can already be used insetup().- The publisher creates the node in its
setup(). The agent that creates a node is its owner. - The subscriber registers a callback with
set_on_item_published()and subscribes to the node. - The
PublishNewsbehaviour publishes two items.publish()returns the ID of each item. - The service sends a notification with each item to every subscriber, and the callback of the subscriber prints it.
- At the end, the publisher deletes the node.
sequenceDiagram
participant P as Publisher
participant S as PubSub service
participant Sub as Subscriber
P->>S: create("news")
Sub->>S: subscribe("news")
P->>S: publish("news", "SPADE PubSub 0.4 released")
S-->>P: item ID
S-)Sub: notification (item)
Note over Sub: on_news(message)
P->>S: delete("news")
The order of the base classes
PubSubMixin must be placed before Agent in the base classes of your agent: class MyAgent(PubSubMixin,
Agent).
Next steps¶
- How it works: nodes, items, roles and the PubSub service.
- Managing nodes, Publishing items and Subscribing and notifications.
- The API reference of all the operations.