Skip to main content

No project description provided

Project description

ReNATS

Elegant, modern and asynchronous NATS Client API library written in pure Python

import asyncio
from renats.client import NATSClient, Message

servers = (
    ("127.0.0.1", 4222),
)

client = await NATSClient().connect(servers)

# Publish message to subject 'my.subject' with payload 'My payload =)'
await client.publish("my.subject", b"My payload =)")

# Publish message to subject 'my.subject' with payload 'My payload =)',
# reply subject 'my.reply.subject' and header 'MyHeader' with value 'MyValue'
await client.publish(
    subject="my.subject",
    payload=b"My payload =)",
    reply_subject="my.reply.subject",
    headers={
        "MyHeader": "MyValue"
    }
)


# Callbacks can be sync or async (def or async def)
def callback(message: Message):
    print(f"Received message in sync callback from {message.subject}: {message.payload}")


async def async_callback(message: Message):
    await asyncio.sleep(3)
    print(f"Received message in async callback from {message.subject}: {message.payload}")


subscription = await client.subscribe("foo.bar", callback)
another_subscription = await client.subscribe("foo.baz", async_callback)

await client.publish("foo.bar", b"Hello world! (to subscription with sync callback)")
await client.publish("foo.baz", b"Hello world! (to subscription with async callback)")

await subscription.unsubscribe()
await another_subscription.unsubscribe()

# Closing client
await client.close()

Installation

pip install renats

TODO

  • Request-Reply
  • JetStream
  • ObjectStorage
  • Request-Reply API Framework for microservices
  • And more, maybe...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

renats-0.2.3a0.tar.gz (19.9 kB view hashes)

Uploaded Source

Built Distribution

renats-0.2.3a0-py3-none-any.whl (13.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page