connect_nkey.py 505 B

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