commit a444dd6e4eff554c8ac282d32c90985d4b07d7ab Author: marys Date: Thu Sep 18 10:55:29 2025 +0200 init version diff --git a/producer.py b/producer.py new file mode 100644 index 0000000..5b484d1 --- /dev/null +++ b/producer.py @@ -0,0 +1,37 @@ + +import asyncio +import nats + +NATS_SERVER = "nats://localhost:4222" +TOKEN = "dfji348934jdd0i24uhjd29834ijrr0345jo0r3j034n" + + +async def message_handler(msg): + subject = msg.subject + reply = msg.reply + data = msg.data.decode() + print(f"Received a message on '{subject} {reply}': {data}") + +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) + + # You can also use the following for TLS against the demo server. + # + # nc = await nats.connect("tls://demo.nats.io:4443") + + + + # Simple publisher and async subscriber via coroutine. + while True: + sub = await nc.subscribe("foo", cb=message_handler) + await asyncio.sleep(0.1) + + + +if __name__ == '__main__': + try: + asyncio.run(main()) + except KeyboardInterrupt: + pass \ No newline at end of file diff --git a/publish.py b/publish.py new file mode 100644 index 0000000..a53c1df --- /dev/null +++ b/publish.py @@ -0,0 +1,29 @@ +import asyncio +import nats + +NATS_SERVER = "nats://localhost:4222" +TOKEN = "dfji348934jdd0i24uhjd29834ijrr0345jo0r3j034n" + + +async def message_handler(msg): + subject = msg.subject + reply = msg.reply + data = msg.data.decode() + print(f"Received a message on '{subject} {reply}': {data}") + +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("foo") + + i = 0 + while True: + msg = await nc.publish("foo", b"message", reply=inbox) + await asyncio.sleep(5) + + +if __name__ == '__main__': + asyncio.run(main()) +