Closed gitdlam closed 8 years ago
Hi @gitdlam thanks for the feedback, I will be adding more examples... Here below there is a snippet of how subscriptions work that I can add to the repo. Let me know if it works for you.
import asyncio
from nats.aio.client import Client as NATS
def run(loop):
nc = NATS()
options = {
"servers": ["nats://127.0.0.1:4222"],
"io_loop": loop
}
yield from nc.connect(**options)
print("Connected to NATS at {}...".format(nc.connected_url.netloc))
@asyncio.coroutine
def subscribe_handler(msg):
subject = msg.subject
reply = msg.reply
data = msg.data.decode()
print("Received a message on '{subject} {reply}': {data}".format(
subject=subject, reply=reply, data=data))
# Basic subscription to receive all published messages
# which are being sent to a single topic 'discover'
yield from nc.subscribe("discover", cb=subscribe_handler)
# Subscription on queue named 'workers' so that
# one subscriber handles a request at a time.
yield from nc.subscribe("help.*", "workers", subscribe_handler)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(run(loop))
try:
loop.run_forever()
finally:
loop.close()
Thanks @wallyqs for the quick response. This example is perfect!!!
Good to hear that it helped, I added a couple more examples to the repo and some basic tools: https://github.com/nats-io/asyncio-nats/blob/master/examples/nats-sub/__main__.py https://github.com/nats-io/asyncio-nats/blob/master/examples/context-manager.py Thanks for the feedback again!
Hi sorry about newbie questioin. I've been messing with the basic examples and can't seem to get the effect i want. I just need a basic example of having a subscription setup and then wait forever, not sure if there is already a built in function for this. The Go version is marvelous but here i'm lost.
Many thanks.