Getting not persistent messages from nats.io server

Viewed 37

My question is simple: Now this code sends empty message to subject chan.01.msg and gets message that is being currently broadcasted or prints nats: timeout. Altogether this request message is also shown (something like: Received a message on chan.01.msg _INBOX.<hash_my>.<salt_up>: b'') on subject and is not desirable there. I do filter it in callback, but I really feel that it's kinda wrong way to do it. Can I just pull messages with desired subject?

async def msgcb(msg):
    """
    Message callback function
    """
    subject = msg.subject
    reply = msg.reply
    data = msg.data
    if len(data) > 0:
        print(f"Received a message on {subject} {reply}: {data}")

logging.debug("Prepare to subscribe")
sub = await nc.subscribe(subject="chan.01.msg", cb=msgcb)

logging.debug("loop process messages on subject")
while True:
    await asyncio.sleep(1)
    try:
        resp = await nc.request('chan.01.msg')
        print(resp)
    except Exception as e:
        print(e)
1 Answers

You are subscribing to the same subject where you are publishing so naturally would get the message when sending a request. To avoid receiving messages the same client produces you can use the no_echo option on connect.

Related