Django NATS Consumer
Project description
django-nats-consumer
NATS + Django = ⚡️
Installation
Please pay attention to the development status; this is Pre-Alpha software; expect the api to evolve as I start using this more in production.
I hope you find some value in it - writing a good consumer takes some finesse.
pip install django-nats-consumer
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.1.0.tar.gz.
File metadata
- Download URL: django_nats_consumer-1.1.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 |
64cf7e3966fb07d8a1a867acbd2144957364b0950cc24eac4775ae07beb583c1
|
|
| MD5 |
20f040a5e5afdacc564fe565939e739d
|
|
| BLAKE2b-256 |
da936f32943761d40afbe45933085087dc9f7f48666c1af4f51bee6ea4365555
|
File details
Details for the file django_nats_consumer-1.1.0-py3-none-any.whl.
File metadata
- Download URL: django_nats_consumer-1.1.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 |
1f67f9dd5b247c6e543bdc800d44365c4a69affa43191203d9b8cd20cbaf7f13
|
|
| MD5 |
fe7d5b58c301a6725447043a95a507eb
|
|
| BLAKE2b-256 |
402e006bc1c9da5cf3d3a296e5d2f49f9622d0bbcf678f54f51a8bae04282ae7
|