Django NATS Consumer
Project description
django-nats-consumer
NATS + Django = ⚡️
Installation
I'm not ready to publish this just yet, so you'll have to install it from source.
This is alpha software, so expect bugs and breaking changes until I release it.
pip install git+https://github.com/dev360/django-nats-consumer.git
Usage
settings.py
INSTALLED_APPS = [
...
"nats_consumer",
]
NATS_CONSUMER = {
"connect_args": {
"servers": ["nats://localhost:4222"],
"allow_reconnect": True,
"max_reconnect_attempts": 5,
"reconnect_time_wait": 1,
"connect_timeout": 10,
},
}
{app_name}/consumers.py
# Consumers need to be in the consumers module in order to be loaded,
# or you can import them to force them to be loaded.
from nats_consumer import JetstreamPushConsumer
import logging
from nats_consumer import JetstreamPushConsumer, operations
logger = logging.getLogger(__name__)
class OrderConsumer(JetstreamPushConsumer):
stream_name = "orders"
subjects = [
"orders.created",
]
# You need to setup the streams
async def setup(self):
return [
operations.CreateStream(
name=self.stream_name,
subjects=self.subjects,
storage="file"
),
]
async def handle_message(self, message):
# The message only shows if its logged as error
logger.error(f"Received message: {message.data}")
publish.py
import asyncio
from nats_consumer import get_nats_client
async def publish_messages():
ns = await get_nats_client()
js = ns.jetstream()
for i in range(5):
data = {"id": i, "name": f"Order {i}"}
data_b = json.dumps(data).encode("utf-8")
print(f"Publishing message {i}...")
await js.publish("orders.created", data_b)
if __name__ == "__main__":
asyncio.run(publish_messages())
Running Consumers
To run a single consumer:
python manage.py nats_consumer OrderConsumer --setup
To run multiple consumers:
python manage.py nats_consumer OrderConsumer AnotherConsumer
To run all consumers:
python manage.py nats_consumer
Feature roadmap
- Encoding/decoding of messages (json, protobuf, etc)
- Better error handling, configurable retry
- Better log output from the consumer
- Configurable DLQ strategies
- [insert your feature here]
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_nats_consumer-1.0.0.tar.gz.
File metadata
- Download URL: django_nats_consumer-1.0.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.0 CPython/3.10.16 Linux/6.8.0-1020-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b72b20c198bc19b786d302d74f860c837ddd7d9dd2fa25fe15bbb4e4329c7cc2
|
|
| MD5 |
c642ba0f4bceea04396533d2012b90ac
|
|
| BLAKE2b-256 |
c06f58876c771172361498ad93dda4772e268263fa3e9245ae5579aad38226b2
|
File details
Details for the file django_nats_consumer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_nats_consumer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.0 CPython/3.10.16 Linux/6.8.0-1020-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62a583a5ab26cefe716a23f0e2c8e324c48a2c232aaee78947f3e2886730c228
|
|
| MD5 |
9bfd98aadfd47b6479372d964fce512d
|
|
| BLAKE2b-256 |
a3db4af42822d79deeb227e9fc3e8dd545308fa39e42988fd2cb469f8160cc9f
|