spade-pubsub 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
spade_pubsub/__init__.py
ADDED
spade_pubsub/pubsub.py
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from xml.etree.ElementTree import Element
|
|
4
|
+
|
|
5
|
+
from loguru import logger
|
|
6
|
+
from slixmpp import ClientXMPP
|
|
7
|
+
from slixmpp.exceptions import IqError, IqTimeout
|
|
8
|
+
from slixmpp.plugins.xep_0004.stanza.form import Form
|
|
9
|
+
from slixmpp.plugins.xep_0060 import XEP_0060
|
|
10
|
+
from slixmpp.stanza import Iq
|
|
11
|
+
|
|
12
|
+
PAYLOAD_NAMESPACE = "spade.pubsub"
|
|
13
|
+
"""Namespace of the ``<payload/>`` element that wraps the text payloads."""
|
|
14
|
+
|
|
15
|
+
# Errors of a request: the server answered with an error, or did not answer in time.
|
|
16
|
+
REQUEST_ERRORS = (IqError, IqTimeout)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class PubSubMixin:
|
|
20
|
+
"""
|
|
21
|
+
This mixin provides PubSub support to SPADE agents.
|
|
22
|
+
It must be used as superclass of a spade.Agent subclass.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
async def _hook_plugin_after_connection(self, *args, **kwargs):
|
|
26
|
+
# SPADE 4.1 calls the hook but does not define it in Agent (4.0 and 4.2 do).
|
|
27
|
+
hook = getattr(super(), "_hook_plugin_after_connection", None)
|
|
28
|
+
if hook is not None:
|
|
29
|
+
await hook(*args, **kwargs)
|
|
30
|
+
self.pubsub = self.PubSubComponent(self.client)
|
|
31
|
+
|
|
32
|
+
class PubSubComponent:
|
|
33
|
+
"""PubSub (XEP-0060) operations of an agent, available as ``agent.pubsub``.
|
|
34
|
+
|
|
35
|
+
The operations are coroutines. When the server answers with an error, or does
|
|
36
|
+
not answer in time, the error is logged and the operation returns ``None`` (or
|
|
37
|
+
an empty list, for the operations that return lists).
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(self, client):
|
|
41
|
+
self.client: ClientXMPP = client
|
|
42
|
+
self.client.register_plugin("xep_0060")
|
|
43
|
+
self.pubsub: XEP_0060 = self.client.plugin["xep_0060"] # Pubsub XEP
|
|
44
|
+
self.client.register_plugin("xep_0004") # Dataforms XEP
|
|
45
|
+
|
|
46
|
+
# OWNER USE CASES
|
|
47
|
+
async def create(
|
|
48
|
+
self,
|
|
49
|
+
target_jid: str,
|
|
50
|
+
target_node: str | None = None,
|
|
51
|
+
config_form: Form | None = None,
|
|
52
|
+
) -> str | None:
|
|
53
|
+
"""
|
|
54
|
+
Create a new node at a service.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
target_jid (str): Address of the PubSub service.
|
|
58
|
+
target_node (str or None): Name of the PubSub node to create (None to
|
|
59
|
+
create an instant node, named by the service).
|
|
60
|
+
config_form (Slixmpp Form): Dataform to configure the node to create.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
The name of the node created, or None on error.
|
|
64
|
+
"""
|
|
65
|
+
try:
|
|
66
|
+
res = await self.pubsub.create_node(
|
|
67
|
+
target_jid, target_node, config=config_form
|
|
68
|
+
)
|
|
69
|
+
if res["pubsub"]["create"]["node"]:
|
|
70
|
+
return res["pubsub"]["create"]["node"]
|
|
71
|
+
return target_node
|
|
72
|
+
except REQUEST_ERRORS as e:
|
|
73
|
+
logger.error(f"Error creating node <{target_node}>: {e}")
|
|
74
|
+
|
|
75
|
+
async def delete(
|
|
76
|
+
self,
|
|
77
|
+
target_jid: str,
|
|
78
|
+
target_node: str | None,
|
|
79
|
+
):
|
|
80
|
+
"""
|
|
81
|
+
Delete an existing node.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
target_jid (str): Address of the PubSub service.
|
|
85
|
+
target_node (str or None): Name of the PubSub node to delete.
|
|
86
|
+
"""
|
|
87
|
+
try:
|
|
88
|
+
return await self.pubsub.delete_node(target_jid, target_node)
|
|
89
|
+
except REQUEST_ERRORS as e:
|
|
90
|
+
logger.error(f"Error deleting node <{target_node}>: {e}")
|
|
91
|
+
|
|
92
|
+
async def get_node_subscriptions(
|
|
93
|
+
self, target_jid: str, target_node: str | None
|
|
94
|
+
) -> list[str]:
|
|
95
|
+
"""
|
|
96
|
+
Return the JIDs subscribed to a node (only the owner of the node can ask).
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
target_jid (str): Address of the PubSub service.
|
|
100
|
+
target_node (str): Name of the node to query.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
The bare JIDs subscribed to the node (an empty list on error).
|
|
104
|
+
"""
|
|
105
|
+
try:
|
|
106
|
+
data: Iq = await self.pubsub.get_node_subscriptions(
|
|
107
|
+
target_jid, target_node
|
|
108
|
+
)
|
|
109
|
+
except REQUEST_ERRORS as e:
|
|
110
|
+
logger.error(
|
|
111
|
+
f"Error retrieving owner subscriptions from node <{target_node}>: {e}"
|
|
112
|
+
)
|
|
113
|
+
return []
|
|
114
|
+
jids = (
|
|
115
|
+
str(sub["jid"])
|
|
116
|
+
for sub in data["pubsub_owner"]["subscriptions"]
|
|
117
|
+
if sub["subscription"] in ("subscribed", "")
|
|
118
|
+
)
|
|
119
|
+
return list(dict.fromkeys(jids))
|
|
120
|
+
|
|
121
|
+
async def purge(self, target_jid: str, target_node: str | None):
|
|
122
|
+
"""
|
|
123
|
+
Delete all items from a node.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
target_jid (str): JID of the PubSub service
|
|
127
|
+
target_node (str): Name of the PubSub node
|
|
128
|
+
"""
|
|
129
|
+
try:
|
|
130
|
+
return await self.pubsub.purge(target_jid, target_node)
|
|
131
|
+
except REQUEST_ERRORS as e:
|
|
132
|
+
logger.error(f"Error purging node <{target_node}>: {e}")
|
|
133
|
+
|
|
134
|
+
async def get_nodes(
|
|
135
|
+
self, target_jid: str, target_node: str | None = None
|
|
136
|
+
) -> list[dict]:
|
|
137
|
+
"""
|
|
138
|
+
Request all nodes at a service or collection node.
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
target_jid (str): Address of the PubSub service.
|
|
142
|
+
target_node (str or None): Name of the collection node to query
|
|
143
|
+
|
|
144
|
+
Returns:
|
|
145
|
+
A list of dicts with the keys ``jid``, ``node`` and ``name`` (an empty
|
|
146
|
+
list on error).
|
|
147
|
+
"""
|
|
148
|
+
try:
|
|
149
|
+
nodes = await self.pubsub.get_nodes(target_jid, target_node)
|
|
150
|
+
except REQUEST_ERRORS as e:
|
|
151
|
+
logger.error(f"Error retrieving nodes: {e}")
|
|
152
|
+
return []
|
|
153
|
+
return [
|
|
154
|
+
{"jid": i[0], "node": i[1], "name": i[2]}
|
|
155
|
+
for i in nodes["disco_items"]["items"]
|
|
156
|
+
]
|
|
157
|
+
|
|
158
|
+
async def get_items(
|
|
159
|
+
self, target_jid: str, target_node: str | None
|
|
160
|
+
) -> list[tuple[str, Element]]:
|
|
161
|
+
"""
|
|
162
|
+
Request all items of a node.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
target_jid (str): Address of the PubSub service.
|
|
166
|
+
target_node (str or None): Name of the PubSub node.
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
A list of tuples ``(id, payload)``, where ``payload`` is the XML element
|
|
170
|
+
published (``payload.text`` is the text of a text payload). An empty
|
|
171
|
+
list if the node has no items, or on error.
|
|
172
|
+
"""
|
|
173
|
+
try:
|
|
174
|
+
data: Iq = await self.pubsub.get_items(target_jid, target_node)
|
|
175
|
+
except REQUEST_ERRORS as e:
|
|
176
|
+
logger.error(f"Error retrieving items from node <{target_node}>: {e}")
|
|
177
|
+
return []
|
|
178
|
+
return [(item["id"], item["payload"]) for item in data["pubsub"]["items"]]
|
|
179
|
+
|
|
180
|
+
# SUBSCRIBER USE CASES
|
|
181
|
+
|
|
182
|
+
async def subscribe(
|
|
183
|
+
self,
|
|
184
|
+
target_jid: str,
|
|
185
|
+
target_node: str | None = None,
|
|
186
|
+
subscription_jid: str | None = None,
|
|
187
|
+
config=None,
|
|
188
|
+
) -> str | None:
|
|
189
|
+
"""
|
|
190
|
+
Subscribe to a node.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
target_jid (str): Address of the PubSub service.
|
|
194
|
+
target_node (str): Name of the PubSub node to subscribe to.
|
|
195
|
+
subscription_jid (str): The address to subscribe to the service.
|
|
196
|
+
config (Data): Optional configuration of the subscription
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
The subscription ID given by the service (None if it gives none, or on
|
|
200
|
+
error).
|
|
201
|
+
"""
|
|
202
|
+
try:
|
|
203
|
+
sub = await self.pubsub.subscribe(
|
|
204
|
+
target_jid, target_node, subscribee=subscription_jid, options=config
|
|
205
|
+
)
|
|
206
|
+
return sub["pubsub"]["subscription"]["subid"] or None
|
|
207
|
+
except REQUEST_ERRORS as e:
|
|
208
|
+
logger.error(f"Error subscribing to node <{target_node}>: {e}")
|
|
209
|
+
|
|
210
|
+
async def unsubscribe(
|
|
211
|
+
self,
|
|
212
|
+
target_jid: str,
|
|
213
|
+
target_node: str | None = None,
|
|
214
|
+
subscription_jid: str | None = None,
|
|
215
|
+
subid=None,
|
|
216
|
+
):
|
|
217
|
+
"""
|
|
218
|
+
Unsubscribe from a node.
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
target_jid (str): Address of the PubSub service.
|
|
222
|
+
target_node (str): Name of the PubSub node to unsubscribe from.
|
|
223
|
+
subscription_jid (str): The address to subscribe from the service.
|
|
224
|
+
subid (str): Unique ID of the subscription to remove.
|
|
225
|
+
"""
|
|
226
|
+
try:
|
|
227
|
+
return await self.pubsub.unsubscribe(
|
|
228
|
+
target_jid,
|
|
229
|
+
target_node,
|
|
230
|
+
subscribee=subscription_jid,
|
|
231
|
+
subid=subid,
|
|
232
|
+
)
|
|
233
|
+
except REQUEST_ERRORS as e:
|
|
234
|
+
logger.error(f"Error unsubscribing to node <{target_node}>: {e}")
|
|
235
|
+
|
|
236
|
+
def set_on_item_published(self, callback):
|
|
237
|
+
"""
|
|
238
|
+
Call ``callback(message)`` when an item is published to a node the agent is
|
|
239
|
+
subscribed to.
|
|
240
|
+
|
|
241
|
+
The item is ``message["pubsub_event"]["items"]["item"]``: its ``"id"`` and its
|
|
242
|
+
``"payload"`` (the XML element published).
|
|
243
|
+
"""
|
|
244
|
+
self.client.add_event_handler("pubsub_publish", callback)
|
|
245
|
+
|
|
246
|
+
def set_on_item_retracted(self, callback):
|
|
247
|
+
"""
|
|
248
|
+
Call ``callback(message)`` when an item is retracted (with ``notify=True``)
|
|
249
|
+
from a node the agent is subscribed to.
|
|
250
|
+
|
|
251
|
+
The ID of the item is ``message["pubsub_event"]["items"]["retract"]["id"]``.
|
|
252
|
+
"""
|
|
253
|
+
self.client.add_event_handler("pubsub_retract", callback)
|
|
254
|
+
|
|
255
|
+
# PUBLISHER USE CASES
|
|
256
|
+
|
|
257
|
+
async def notify(self, target_jid: str, target_node: str):
|
|
258
|
+
"""
|
|
259
|
+
Notify all subscribers of a node without publishing an item.
|
|
260
|
+
“Publish” to the node at jid without any item. This merely fans out a notification.
|
|
261
|
+
|
|
262
|
+
Only transient, notification-only nodes accept publishing without an item
|
|
263
|
+
(XEP-0060 §7.1.3.6): other nodes answer with an ``item-required`` error.
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
target_jid (str): Address of the PubSub service.
|
|
267
|
+
target_node (str): Name of the PubSub node to send a notify from.
|
|
268
|
+
"""
|
|
269
|
+
try:
|
|
270
|
+
await self.pubsub.publish(target_jid, target_node)
|
|
271
|
+
except REQUEST_ERRORS as e:
|
|
272
|
+
logger.error(f"Error notifying to node <{target_node}>: {e}")
|
|
273
|
+
|
|
274
|
+
async def publish(
|
|
275
|
+
self,
|
|
276
|
+
target_jid: str,
|
|
277
|
+
target_node: str,
|
|
278
|
+
payload: Element | str,
|
|
279
|
+
item_id: str | None = None,
|
|
280
|
+
ifrom: str | None = None,
|
|
281
|
+
) -> str | None:
|
|
282
|
+
"""
|
|
283
|
+
Publish an item to a node.
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
target_jid (str): Address of the PubSub service.
|
|
287
|
+
target_node (str): Name of the PubSub node to publish to.
|
|
288
|
+
payload (Element | str): Payload to publish. A text is sent inside a
|
|
289
|
+
``<payload xmlns="spade.pubsub"/>`` element; an XML element is sent
|
|
290
|
+
as it is.
|
|
291
|
+
item_id (str or None): Item ID to use for the item (None to let the
|
|
292
|
+
service give one).
|
|
293
|
+
|
|
294
|
+
Returns:
|
|
295
|
+
The ID of the item published, or None on error.
|
|
296
|
+
"""
|
|
297
|
+
if isinstance(payload, Element):
|
|
298
|
+
payload_stanza = payload
|
|
299
|
+
else:
|
|
300
|
+
payload_stanza = Element(f"{{{PAYLOAD_NAMESPACE}}}payload")
|
|
301
|
+
payload_stanza.text = payload
|
|
302
|
+
try:
|
|
303
|
+
res = await self.pubsub.publish(
|
|
304
|
+
target_jid, target_node, item_id, payload_stanza, ifrom=ifrom
|
|
305
|
+
)
|
|
306
|
+
except REQUEST_ERRORS as e:
|
|
307
|
+
logger.error(
|
|
308
|
+
f"Error publishing item <{item_id or 'undefined'}> to node <{target_node}>: {e}"
|
|
309
|
+
)
|
|
310
|
+
return None
|
|
311
|
+
return res["pubsub"]["publish"]["item"]["id"] or item_id
|
|
312
|
+
|
|
313
|
+
async def retract(
|
|
314
|
+
self, target_jid: str, target_node: str, item_id: str, notify=False
|
|
315
|
+
):
|
|
316
|
+
"""
|
|
317
|
+
Retract a previously published item from a node.
|
|
318
|
+
|
|
319
|
+
Args:
|
|
320
|
+
target_jid (str): Address of the PubSub service.
|
|
321
|
+
target_node (str): Name of the PubSub node to send a notify from.
|
|
322
|
+
item_id (str): The ID of the item to retract.
|
|
323
|
+
notify (bool): Flag indicating whether subscribers shall be notified about the retraction.
|
|
324
|
+
"""
|
|
325
|
+
try:
|
|
326
|
+
return await self.pubsub.retract(
|
|
327
|
+
target_jid, target_node, item_id, notify
|
|
328
|
+
)
|
|
329
|
+
except REQUEST_ERRORS as e:
|
|
330
|
+
logger.error(
|
|
331
|
+
f"Error retracting item <{item_id}> to node <{target_node}>: {e}"
|
|
332
|
+
)
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spade_pubsub
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: SPADE plugin for PubSub (XEP-0060) support
|
|
5
|
+
Author-email: Javi Palanca <jpalanca@gmail.com>
|
|
6
|
+
Maintainer-email: Javi Palanca <jpalanca@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://spadeagents.eu
|
|
9
|
+
Project-URL: Repository, https://github.com/javipalanca/spade_pubsub
|
|
10
|
+
Project-URL: Documentation, https://spadeagents.eu/docs/spade_pubsub/
|
|
11
|
+
Keywords: spade,xmpp,pubsub,xep-0060,multi-agent systems
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Natural Language :: English
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
21
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
22
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Classifier: Topic :: Internet :: XMPP
|
|
25
|
+
Requires-Python: <3.15,>=3.11
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: spade>=4.2.0
|
|
29
|
+
Requires-Dist: loguru>=0.7.0
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: bump-my-version==1.8.7; extra == "dev"
|
|
32
|
+
Requires-Dist: ruff==0.17.6; extra == "dev"
|
|
33
|
+
Requires-Dist: coverage==7.16.2; extra == "dev"
|
|
34
|
+
Requires-Dist: pytest==9.2.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pytest-asyncio==1.5.0; extra == "dev"
|
|
36
|
+
Requires-Dist: factory-boy==3.3.3; extra == "dev"
|
|
37
|
+
Requires-Dist: pyjabber>=0.5.1; extra == "dev"
|
|
38
|
+
Provides-Extra: docs
|
|
39
|
+
Requires-Dist: mkdocs==1.6.1; extra == "docs"
|
|
40
|
+
Requires-Dist: mkdocs-material==9.8.2; extra == "docs"
|
|
41
|
+
Requires-Dist: mkdocstrings[python]==1.0.8; extra == "docs"
|
|
42
|
+
Requires-Dist: mkdocstrings-python==2.0.11; extra == "docs"
|
|
43
|
+
Requires-Dist: pymdown-extensions==12.3; extra == "docs"
|
|
44
|
+
Dynamic: license-file
|
|
45
|
+
|
|
46
|
+
<p align="center">
|
|
47
|
+
<img src="https://raw.githubusercontent.com/javipalanca/spade/master/docs/assets/images/spade_logo.png" alt="SPADE" width="360">
|
|
48
|
+
</p>
|
|
49
|
+
|
|
50
|
+
<h1 align="center">SPADE PubSub</h1>
|
|
51
|
+
|
|
52
|
+
<p align="center">
|
|
53
|
+
<b>Publish-subscribe communication for <a href="https://spadeagents.eu">SPADE</a> agents</b>, with the XMPP
|
|
54
|
+
Publish-Subscribe extension (<a href="https://xmpp.org/extensions/xep-0060.html">XEP-0060</a>).
|
|
55
|
+
</p>
|
|
56
|
+
|
|
57
|
+
<p align="center">
|
|
58
|
+
<a href="https://pypi.org/project/spade_pubsub/"><img src="https://img.shields.io/pypi/v/spade_pubsub.svg" alt="PyPI"></a>
|
|
59
|
+
<a href="https://pypi.org/project/spade_pubsub/"><img src="https://img.shields.io/pypi/pyversions/spade_pubsub.svg" alt="Python versions"></a>
|
|
60
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/pypi/l/spade_pubsub" alt="MIT License"></a>
|
|
61
|
+
<a href="https://pepy.tech/project/spade_pubsub"><img src="https://pepy.tech/badge/spade_pubsub" alt="Downloads"></a>
|
|
62
|
+
<a href="https://github.com/javipalanca/spade_pubsub/actions/workflows/python-package.yml"><img src="https://github.com/javipalanca/spade_pubsub/actions/workflows/python-package.yml/badge.svg" alt="Continuous Integration"></a>
|
|
63
|
+
<a href="https://coveralls.io/github/javipalanca/spade_pubsub?branch=master"><img src="https://coveralls.io/repos/github/javipalanca/spade_pubsub/badge.svg?branch=master" alt="Coverage"></a>
|
|
64
|
+
<a href="https://spadeagents.eu/docs/spade_pubsub/"><img src="https://img.shields.io/badge/docs-spadeagents.eu-2f6bff" alt="Documentation"></a>
|
|
65
|
+
</p>
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
Agents publish items in **nodes** of an XMPP PubSub service, and the agents **subscribed** to a node are notified of
|
|
70
|
+
every new item. Publishers and subscribers do not need to know each other: a decoupled, one-to-many communication
|
|
71
|
+
for your multi-agent systems.
|
|
72
|
+
|
|
73
|
+
📖 **Documentation:** [stable](https://spadeagents.eu/docs/spade_pubsub/) ·
|
|
74
|
+
[develop](https://spadeagents.eu/docs/spade_pubsub/develop/)
|
|
75
|
+
|
|
76
|
+
## Features
|
|
77
|
+
|
|
78
|
+
- **Nodes**: create and delete nodes, list the nodes of a service and their subscribers, and purge their items.
|
|
79
|
+
- **Items**: publish text or any XML element, read the items stored in a node, and retract them.
|
|
80
|
+
- **Notifications**: register a callback, and your agent is notified as soon as an item is published or retracted.
|
|
81
|
+
- **Standard XMPP** ([XEP-0060](https://xmpp.org/extensions/xep-0060.html)): it works with the XMPP server built into
|
|
82
|
+
SPADE and with servers such as Prosody, ejabberd or Openfire.
|
|
83
|
+
|
|
84
|
+
## Installation
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
pip install spade_pubsub
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
or, with [uv](https://docs.astral.sh/uv/):
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
uv add spade_pubsub
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
SPADE PubSub requires Python 3.11 or later and SPADE 4.2 or later.
|
|
97
|
+
|
|
98
|
+
## Example
|
|
99
|
+
|
|
100
|
+
Add `PubSubMixin` to the base classes of your agent (before `Agent`), and use `self.pubsub`:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
import asyncio
|
|
104
|
+
|
|
105
|
+
import spade
|
|
106
|
+
from spade.agent import Agent
|
|
107
|
+
|
|
108
|
+
from spade_pubsub import PubSubMixin
|
|
109
|
+
|
|
110
|
+
PUBSUB = "pubsub.localhost" # the PubSub service of the XMPP server
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class PublisherAgent(PubSubMixin, Agent):
|
|
114
|
+
async def setup(self):
|
|
115
|
+
await self.pubsub.create(PUBSUB, "news")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class SubscriberAgent(PubSubMixin, Agent):
|
|
119
|
+
def on_news(self, message):
|
|
120
|
+
item = message["pubsub_event"]["items"]["item"]
|
|
121
|
+
print(f"News: {item['payload'].text}")
|
|
122
|
+
|
|
123
|
+
async def setup(self):
|
|
124
|
+
self.pubsub.set_on_item_published(self.on_news)
|
|
125
|
+
await self.pubsub.subscribe(PUBSUB, "news")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
async def main():
|
|
129
|
+
publisher = PublisherAgent("publisher@localhost", "secret")
|
|
130
|
+
subscriber = SubscriberAgent("subscriber@localhost", "secret")
|
|
131
|
+
await publisher.start()
|
|
132
|
+
await subscriber.start()
|
|
133
|
+
|
|
134
|
+
await publisher.pubsub.publish(PUBSUB, "news", "SPADE PubSub 0.4 released")
|
|
135
|
+
await asyncio.sleep(1) # the notification is on its way
|
|
136
|
+
|
|
137
|
+
await publisher.pubsub.delete(PUBSUB, "news")
|
|
138
|
+
await subscriber.stop()
|
|
139
|
+
await publisher.stop()
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
if __name__ == "__main__":
|
|
143
|
+
spade.run(main(), embedded_xmpp_server=True) # with the XMPP server of SPADE
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
See the [quick start](https://spadeagents.eu/docs/spade_pubsub/getting-started/quickstart/) and the
|
|
147
|
+
[guides](https://spadeagents.eu/docs/spade_pubsub/guides/concepts/).
|
|
148
|
+
|
|
149
|
+
## Contributing
|
|
150
|
+
|
|
151
|
+
Contributions are welcome: see [CONTRIBUTING.md](CONTRIBUTING.md) and the [history of changes](HISTORY.md).
|
|
152
|
+
|
|
153
|
+
## License and credits
|
|
154
|
+
|
|
155
|
+
SPADE PubSub is free software under the [MIT license](LICENSE), developed at the
|
|
156
|
+
[VRAIN](https://vrain.upv.es) institute of the Universitat Politècnica de València. See [AUTHORS.md](AUTHORS.md).
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
spade_pubsub/__init__.py,sha256=e2SVYurn-TKsNKNgE0GK2Gt1sLIcu_zD14JePD4ycwA,208
|
|
2
|
+
spade_pubsub/pubsub.py,sha256=4BL9nKk0wEXNQp0U9d2cOoNep5s8OGJh1FXByMm3gWY,12937
|
|
3
|
+
spade_pubsub-0.4.0.dist-info/licenses/LICENSE,sha256=geWBDybRuLuHm3CjBv9lu5O42qq05JAHhJZkS49fsBM,1071
|
|
4
|
+
spade_pubsub-0.4.0.dist-info/METADATA,sha256=2EAl_220pEflhERfkIDQd_53Qh1NmdlliThW1T0fNbg,6414
|
|
5
|
+
spade_pubsub-0.4.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
spade_pubsub-0.4.0.dist-info/top_level.txt,sha256=_21kbXHSg1A1m9nAkB3zqhcFUltznMcI6eNRrmlFEhQ,13
|
|
7
|
+
spade_pubsub-0.4.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021, Javi Palanca
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
spade_pubsub
|