github nats-io/nats.py nats-core/v0.1.0

6 hours ago

Initial release of nats-core, a lean and fast Python client for the NATS messaging system.

Features

  • Publish/Subscribe - Core pub/sub messaging with wildcard subscriptions
  • Request/Reply - Synchronous request-response pattern with timeout support
  • Queue Groups - Load balancing across subscribers
  • Message Headers - Multi-value header support (NATS 2.2+)
  • Automatic Reconnection - Configurable reconnect with backoff and jitter
  • TLS Support - Secure connections with custom SSL contexts
  • Authentication - Token, username/password, NKey, and JWT/credentials file support
  • Async Context Manager - Clean resource management with async with
  • Connection Statistics - Track messages/bytes sent and received
  • Slow Consumer Detection - Configurable pending limits with callbacks

Requirements

  • Python 3.13+
  • No required dependencies (optional nkeys for NKey authentication)

Installation

# For now, this is standalone outside of the nats-py package.
pip install nats-core

Quick Start

import asyncio

from nats.client import connect

async def main():
    async with await connect("nats://localhost:4222") as client:
        sub = await client.subscribe("greet")
        await client.publish("greet", b"Hello, NATS!")
        msg = await sub.next()
        print(f"Received: {msg.data.decode()}")

asyncio.run(main())

Performance

Performance is substantially improved compared to nats-py.

Benchmarks on Apple M3 Max (1M messages, publisher and subscriber in same process):

Queue Mode (subscriber throughput):

Size nats-core CPython nats-core PyPy nats-py CPython nats-py PyPy
8 B 553,636 msg/s 2,336,040 msg/s 8,769 msg/s* 8,755 msg/s*
128 B 418,524 msg/s 1,402,979 msg/s 8,758 msg/s* 8,756 msg/s*
1 KB 338,365 msg/s 493,128 msg/s 2,232 msg/s* 2,232 msg/s*

* nats-py dropped 47-87% of messages under load

Zero message loss with nats-core across all configurations.

Don't miss a new nats.py release

NewReleases is sending notifications on new releases.