publish_json.py 409 B

1234567891011121314151617181920
  1. import asyncio
  2. import json
  3. from nats.aio.client import Client as NATS
  4. async def example():
  5. # [begin publish_json]
  6. nc = NATS()
  7. await nc.connect(servers=["nats://demo.nats.io:4222"])
  8. await nc.publish("updates", json.dumps({"symbol": "GOOG", "price": 1200 }).encode())
  9. # [end publish_json]
  10. await nc.close()
  11. loop = asyncio.get_event_loop()
  12. loop.run_until_complete(example())
  13. loop.close()