# Django NATS
[](https://github.com/C0D1UM/django-nats-client/blob/main/LICENSE)
[](https://github.com/C0D1UM/django-nats-client/actions/workflows/ci.yml)
[](https://codecov.io/gh/C0D1UM/django-nats-client)
[](https://pypi.org/project/django-nats-client/)
[](https://github.com/C0D1UM/django-nats-client)
## Features
- Wrapper of NATS's [nats-py](https://github.com/nats-io/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
```bash
pip install django-nats-client
```
## Setup
1. Add `nats_client` into `INSTALLED_APPS`
```python
# settings.py
INSTALLED_APPS = [
...
'nats_client',
]
```
1. Put NATS connection configuration in settings
```python
# settings.py
NATS_OPTIONS = {
'servers': ['nats://localhost:4222'],
'max_reconnect_attempts': 2,
'connect_timeout': 1,
...
}
NATS_LISTENING_SUBJECT = 'default'
```
## Usage
### Listen for messages
1. Create a new callback method and register
```python
# common/nats.py
import nats_client
@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)
```
1. Import previously file in `ready` method of your `apps.py`
```python
# common/apps.py
class CommonConfig(AppConfig):
...
def ready(self):
import common.nats
```
1. Run listener management command
```bash
python manage.py nats_listener
# or with autoreload enabled (suite for development)
python manage.py nats_listener --reload
```
### Sending message
```python
import nats_client
arg = 'some arg'
nats_client.send(
'subject_name',
'method_name',
arg,
keyword_arg=1,
another_keyword_arg=2,
)
```
Examples
```python
import nats_client
nats_client.send('default', 'new_message', 'Hello, world!')
nats_client.send('default', 'project_created', 1, name='ACME')
```
### Request-Reply
```python
import nats_client
arg = 'some arg'
nats_client.request(
'subject_name',
'method_name',
arg,
keyword_arg=1,
another_keyword_arg=2,
)
```
Examples
```python
import nats_client
year = nats_client.request('default', 'get_year_from_date', datetime.date(2022, 1, 1)) # 2022
current_time = nats_client.request('default', 'get_current_time') # 12:11
```
## Settings
| Key | Required | Default | Description |
|--------------------------|----------|-----------|---------------------------------------------------|
| `NATS_OPTIONS` | Yes | | Configuration to be passed in `nats.connect()` |
| `NATS_LISTENING_SUBJECT` | No | 'default' | Subject for registering callback function |
| `NATS_REQUEST_TIMEOUT` | No | 1 | Timeout when using `request()` (in seconds) |
## Development
### Requirements
- Docker
- Python
- Poetry
### Linting
```bash
make lint
```
### Testing
```bash
make test
```
### Fix Formatting
```bash
make yapf
```