ping_20s.py 498 B

12345678910111213141516171819202122232425262728
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin ping_20s]
  5. nc = NATS()
  6. await nc.connect(
  7. servers=["nats://demo.nats.io:4222"],
  8. # Set Ping Interval to 20 seconds
  9. ping_interval=20,
  10. )
  11. # Do something with the connection.
  12. # [end ping_20s]
  13. while True:
  14. if nc.is_closed:
  15. break
  16. await asyncio.sleep(1)
  17. await nc.close()
  18. loop = asyncio.get_event_loop()
  19. loop.run_until_complete(example())
  20. loop.close()