no_echo.py 686 B

12345678910111213141516171819202122232425262728293031
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin no_echo]
  5. ncA = NATS()
  6. ncB = NATS()
  7. await ncA.connect(no_echo=True)
  8. await ncB.connect()
  9. async def handler(msg):
  10. # Messages sent by `ncA' will not be received.
  11. print("[Received] ", msg)
  12. await ncA.subscribe("greetings", cb=handler)
  13. await ncA.flush()
  14. await ncA.publish("greetings", b'Hello World!')
  15. await ncB.publish("greetings", b'Hello World!')
  16. # Do something with the connection
  17. await asyncio.sleep(1)
  18. await ncA.drain()
  19. await ncB.drain()
  20. # [end no_echo]
  21. loop = asyncio.get_event_loop()
  22. loop.run_until_complete(example())
  23. loop.close()