connect_creds.py 508 B

123456789101112131415161718192021222324
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin connect_creds]
  5. nc = NATS()
  6. async def error_cb(e):
  7. print("Error:", e)
  8. await nc.connect("nats://localhost:4222",
  9. user_credentials="path_to_creds_file",
  10. error_cb=error_cb,
  11. )
  12. # Do something with the connection
  13. await nc.close()
  14. # [end connect_creds]
  15. loop = asyncio.get_event_loop()
  16. loop.run_until_complete(example())
  17. loop.close()