Django NATS Client
Important Notes
🚨 The latest major version of this package,
v0.4.0, has a numerous breaking changes. Please review its release note.
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 publishing NATS messages
- Support NATS JetStream pull subscription
Installation
pip install django-nats-client
Setup
-
Add
nats_clientintoINSTALLED_APPS# settings.py INSTALLED_APPS = [ ... 'nats_client', ]
-
Put NATS connection configuration in settings
# settings.py NATS_SERVERS = 'nats://localhost:4222' NATS_NAMESPACE = 'foo'
Usage
Listen for messages
-
Create a new callback method and register
# common/nats.py import nats_client @nats_client.register def new_message(message: str): print(message) @nats_client.register def get_year_from_date(date: str): return date.year # custom name @nats_client.register('get_current_time') def current_time(): return datetime.datetime.now().strftime('%H:%M') # without decorator def current_time(): return datetime.datetime.now().strftime('%H:%M') nats_client.register('get_current_time', current_time) # JetStream @nats_client.register(js=True) def new_message(message: str): print(message) # JetStream from other namespace @nats_client.register(namespace='bar', js=True) def new_message_from_bar(message: str): print(message)
-
Import previously file in
readymethod of yourapps.py# common/apps.py class CommonConfig(AppConfig): ... def ready(self): import common.nats
-
Run listener management command
python manage.py nats_listener # or with autoreload enabled (suite for development) python manage.py nats_listener --reload
Publishing message
import nats_client
arg = 'some arg'
await nats_client.publish(
'subject_name',
'method_name',
arg,
keyword_arg=1,
another_keyword_arg=2,
)
Examples
import nats_client
await nats_client.publish('default', 'new_message', 'Hello, world!')
await nats_client.publish('default', 'project_created', 1, name='ACME')
# JetStream
await nats_client.publish('default', 'new_message', 'Hello, world!', _js=True)
await nats_client.js_publish('default', 'new_message', 'Hello, world!')
Request-Reply
import nats_client
arg = 'some arg'
await nats_client.request(
'subject_name',
'method_name',
arg,
keyword_arg=1,
another_keyword_arg=2,
)
Examples
import nats_client
year = await nats_client.request('default', 'get_year_from_date', datetime.date(2022, 1, 1)) # 2022
current_time = await nats_client.request('default', 'get_current_time') # 12:11
Settings
| Key | Type | Required | Default | Description |
|---|---|---|---|---|
NATS_SERVER |
str |
Required if no NATS_SERVERS |
NATS server's host | |
NATS_SERVERS |
list[str] |
Required if no NATS_SERVER |
NATS server's hosts (for NATS cluster) | |
NATS_NAMESPACE |
str |
No | 'default' |
Main namespace using for prefixing subject, stream name, and etc. |
NATS_REQUEST_TIMEOUT |
int |
No | 1 |
Timeout when using request() (in seconds) |
NATS_OPTIONS |
dict |
No | {} |
Other configuration to be passed in nats.connect() |
NATS_JETSTREAM_ENABLED |
bool |
No | True |
Enable JetStream |
NATS_JETSTREAM_DURABLE_NAME |
str |
No | settings.NATS_NAMESPACE |
Durable name which is unique across all subscriptions |
NATS_JETSTREAM_CREATE_STREAM |
bool |
No | True |
Automatically create stream named in NATS_NAMESPACE |
NATS_JETSTREAM_CONFIG |
dict |
No | {} |
Extra configuration for JetStream streams |
Development
Requirements
- Docker
- Python
- Poetry
Linting
make lint
Testing
make test
Fix Formatting
make yapf
Metadata
Release files for django-nats-client 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_nats_client-0.4.3.tar.gz | 8.5 kB | Without provenance | Details for django_nats_client-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_nats_client-0.4.3-py3-none-any.whl | Python 3 | none | any | Details for django_nats_client-0.4.3-py3-none-any.whl |
Total release size: 17.7 kB
Release files / django_nats_client-0.4.3.tar.gz
| Download URL | django_nats_client-0.4.3.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86b4301b1a5e50197ef6ec97fba7e0ac6d91916a5856497d2faa08854d757b3e
|
|
BLAKE2b-256 checksum How to use checksums |
2ebcde74ac71197b254e133ceb8dceec521da6c62112ad2b17b8490d3cec9ad3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.13 Linux/6.2.0-1019-azure
|
Release files / django_nats_client-0.4.3-py3-none-any.whl
| Download URL | django_nats_client-0.4.3-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b6a0e63bcf44b8bd7fcdb44256d20bcb6e9236bb87d21d923f9c63b0dbaad6dc
|
|
BLAKE2b-256 checksum How to use checksums |
a768e86f0bf250906943451704f6dc28445791a0c366b896562c4c62b23e3fcd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.13 Linux/6.2.0-1019-azure
|
Security for django-nats-client 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.