Metadata-Version: 2.4
Name: nats-py-sync
Version: 0.1.1
Summary: Synchronous wrapper around the native nats async library
Author-email: "l.weiler" <lucas.l.weiler@leidos.com>
Maintainer-email: "l.weiler" <lucas.l.weiler@leidos.com>
License: MIT License
        
        Copyright (c) 2025 l.weiler
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: nats-py~=2.11
Description-Content-Type: text/markdown

# nats-py-sync

Synchronous wrapper around native [nats-py](https://github.com/nats-io/nats.py) async library

This is not a comprehensive, feature complete wrapper. It has the main features described in the [nats-py](https://github.com/nats-io/nats.py) readme

---
> [!TIP] Who is this library for?
> This library if mainly useful for people who need nats functionality in their non-async based python code. If you can use async in your codebase, use nats-py directly

---
> [!TIP] How does this library work?
> The NATSContext object maintains its own event loop and queries nats.NATSConnection tasks to complete and return.
> NATSContext calls are blocking until the nats.NATSConnection futures finish

## Installation

```shell
pip install nats-py-sync
```

## Publisher/Subscriber

```python
# subscriber
import nats_sync


nc = nats_sync.connect("nats://localhost:4222")
sub = nc.subscribe("hello")
while True:
    msg = sub.recv()
    print(f"Received {msg.data.decode()} on topic {msg.subject}")
```

```python
# publisher
import time
import nats_sync


NATS_TOPIC = "hello"

nc = nats_sync.connect("nats://localhost:4222")
while True:
    nc.publish(NATS_TOPIC, b"hello world")
    print(f"Published message on {NATS_TOPIC}")
    time.sleep(0.1)
```

## Request/Response

```python
# request
import time
import nats_sync


client = nats_sync.connect("nats://localhost:4222")
while True:
    msg = client.request("request", "hello world".encode())
    print(f"Received Reply {msg.data.decode()} on topic {msg.subject}")
    time.sleep(1.0)
```

```python
# response
import nats_sync


nc = nats_sync.connect("nats://localhost:4222")
sub = nc.subscribe("request")
while True:
    msg = sub.recv()
    print(f"Received {msg.data.decode()} on topic {msg.subject}")
    sub.respond(msg, "Reply".encode())
```
