No project description provided
Project description
Django NATS
Features
- Wrapper of NATS's nats-py
- Django management command to listen for incoming NATS messages
- Automatically serialize/deserialize message from/to JSON format
- Easy-to-call method for sending NATS messages
Installation
pip install django-nats-client
Setup
-
Add
nats_client
intoINSTALLED_APPS
# settings.py INSTALLED_APPS = [ ... 'nats_client', ]
-
Put NATS connection configuration in settings
# settings.py NATS_OPTIONS = { 'servers': ['nats://localhost:4222'], 'max_reconnect_attempts': 2, 'connect_timeout': 1, ... } NATS_DEFAULT_SUBJECT = 'default'
Usage
Listen for messages
-
Create a new callback method and register
# common/nats_callback.py import nats_client @nats_client.register def get_year_from_date(date: str): return date.year # custom subject @nats_client.register('subject') def current_time(): return datetime.datetime.now().strftime('%H:%M') # custom method name @nats_client.register('subject', 'get_current_time') def current_time(): return datetime.datetime.now().strftime('%H:%M')
-
Run listener management command
python manage.py nats_listener
Sending message
import nats_client
arg = 'some arg'
nats_client.send(
'subject_name',
'method_name',
arg,
keyword_arg=1,
another_keyword_arg=2,
)
Examples
import nats_client
year = nats_client.send('default', 'get_year_from_date', datetime.date(2022, 1, 1)) # 2022
current_time = nats_client.send('default', 'get_current_time') # 12:11
Settings
Key | Required | Default | Description |
---|---|---|---|
NATS_OPTIONS |
Yes | Configuration to be passed in nats.connect() |
|
NATS_DEFAULT_SUBJECT |
No | 'default' | Default subject for registering callback function |
Development
Requirements
- Docker
- Python
- Poetry
Linting
make lint
Testing
make test
Fix Formatting
make yapf
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for django_nats_client-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50827f7b80d5d057d4c63443d8d566903889c0eeee9f73768b814c11819e403a |
|
MD5 | 24510c0e8ff81d6a2d7cee6869984dfd |
|
BLAKE2b-256 | 22a0844d82e20e1b3acb34f1f21462d66a3cb40224201da959ace4307b243ab9 |