| #!/usr/bin/env python3 | |
| """ | |
| NATS bus wrapper (optional). Uses nats-py if available. | |
| """ | |
| from typing import Any, Dict | |
| async def publish(url: str, subject: str, payload: Dict[str, Any]) -> bool: | |
| try: | |
| from nats.aio.client import Client as NATS # type: ignore | |
| import json | |
| nc = NATS() | |
| await nc.connect(servers=[url]) | |
| await nc.publish(subject, json.dumps(payload).encode("utf-8")) | |
| await nc.flush() | |
| await nc.drain() | |
| return True | |
| except Exception: | |
| return False | |