nats.py

Python3 client for the NATS messaging system.

Getting Started

pip install nats-py

You can also optionally install nkeys in order to use the NATS v2.0 decentralized auth features using JWTs.

pip install nats-py[nkeys]

NATS Hello World in Python 🐍

import asyncio
import nats

async def main():
    # Connect to NATS!
    nc = await nats.connect("demo.nats.io")

    # Receive messages on 'foo'
    sub = await nc.subscribe("foo")

    # Publish a message to 'foo'
    await nc.publish("foo", b'Hello from Python!')

    # Process a message
    msg = await sub.next_msg()
    print("Received:", msg)

    # Make sure all published messages have reached the server
    await nc.flush()

    # Close NATS connection
    await nc.close()

if __name__ == '__main__':
    asyncio.run(main())

Documentation

Site

License

Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.