working version
This commit is contained in:
28
producer.py
28
producer.py
@@ -5,30 +5,26 @@ import nats
|
|||||||
NATS_SERVER = "nats://localhost:4222"
|
NATS_SERVER = "nats://localhost:4222"
|
||||||
TOKEN = "dfji348934jdd0i24uhjd29834ijrr0345jo0r3j034n"
|
TOKEN = "dfji348934jdd0i24uhjd29834ijrr0345jo0r3j034n"
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
|
||||||
async def message_handler(msg):
|
async def main():
|
||||||
|
nc = await nats.connect(NATS_SERVER, token=TOKEN)
|
||||||
|
|
||||||
|
async def message_handler(msg):
|
||||||
subject = msg.subject
|
subject = msg.subject
|
||||||
reply = msg.reply
|
reply = msg.reply
|
||||||
data = msg.data.decode()
|
data = msg.data.decode()
|
||||||
|
global count
|
||||||
print(f"Received a message on '{subject} {reply}': {data}")
|
print(f"Received a message on '{subject} {reply}': {data}")
|
||||||
|
await nc.publish(reply, f"response-{count}".encode("utf-8"))
|
||||||
async def main():
|
count = count + 1
|
||||||
# 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.
|
# Simple publisher and async subscriber via coroutine.
|
||||||
|
await nc.subscribe("foo", cb=message_handler)
|
||||||
|
await nc.subscribe("foo2", cb=message_handler)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
sub = await nc.subscribe("foo", cb=message_handler)
|
await asyncio.sleep(1)
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
|
|||||||
20
publish.py
20
publish.py
@@ -1,29 +1,31 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from http.client import responses
|
||||||
|
|
||||||
import nats
|
import nats
|
||||||
|
|
||||||
NATS_SERVER = "nats://localhost:4222"
|
NATS_SERVER = "nats://localhost:4222"
|
||||||
TOKEN = "dfji348934jdd0i24uhjd29834ijrr0345jo0r3j034n"
|
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():
|
async def main():
|
||||||
# It is very likely that the demo server will see traffic from clients other than yours.
|
# 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.
|
# To avoid this, start your own locally and modify the example to use it.
|
||||||
nc = await nats.connect(NATS_SERVER, token=TOKEN)
|
nc = await nats.connect(NATS_SERVER, token=TOKEN)
|
||||||
inbox = nc.new_inbox()
|
inbox = nc.new_inbox()
|
||||||
sub = await nc.subscribe("foo")
|
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
|
i = 0
|
||||||
while True:
|
while True:
|
||||||
msg = await nc.publish("foo", b"message", reply=inbox)
|
response = await nc.request("foo", b"message")
|
||||||
|
print(f"{response=}")
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user