Files
nats/publish.py
2025-09-18 13:01:27 +02:00

32 lines
855 B
Python

import asyncio
from http.client import responses
import nats
NATS_SERVER = "nats://localhost:4222"
TOKEN = "dfji348934jdd0i24uhjd29834ijrr0345jo0r3j034n"
async def main():
# It is very likely that the demo server will see traffic from clients other than yours.
# To avoid this, start your own locally and modify the example to use it.
nc = await nats.connect(NATS_SERVER, token=TOKEN)
inbox = nc.new_inbox()
sub = await nc.subscribe("foo2")
async def message_handler(msg):
subject = msg.subject
reply = msg.reply
data = msg.data.decode()
print(f"Received a message on '{subject} {reply}': {data}")
i = 0
while True:
response = await nc.request("foo", b"message")
print(f"{response=}")
await asyncio.sleep(5)
if __name__ == '__main__':
asyncio.run(main())