Skip to main content

nest-rpc-client

PyPI Python

Async Python client for RPC transport interoperability with NestJS microservices. Supports RabbitMQ, Redis, NATS, TCP (and planned support for Kafka, MQTT).

Features

  • Fully async transports
  • Pluggable transport architecture
  • Compatible with NestJS microservices patterns (send, emit)
  • Extras-based installation

Installation

Install with a specific transport:

# TCP uses asyncio
pip install "nest-rpc-client[rabbitmq]"
pip install "nest-rpc-client[redis]"
pip install "nest-rpc-client[nats]"
pip install "nest-rpc-client[all]"

Usage

from nest_rpc_client.client import Client
from nest_rpc_client.transports.rabbitmq import RabbitMQTransport
from nest_rpc_client.config.rabbitmq import RabbitMQConfig

transport = RabbitMQTransport(RabbitMQConfig(url="amqp://guest:guest@localhost/", queue="rpc_queue"))

async with Client(transport) as client:
    result = await client.send("get_user", {"id": 123})
    await client.emit("user_created", {"id": 123})

You can also use only transport:

from nest_rpc_client.transports.rabbitmq import RabbitMQTransport
from nest_rpc_client.config.rabbitmq import RabbitMQConfig

transport = RabbitMQTransport(RabbitMQConfig(url="amqp://guest:guest@localhost/", queue="rpc_queue"))

await transport.connect()

result = await transport.send("get_user", {"id": 123})
await transport.emit("user_created", {"id": 123})

await transport.close()

Pattern usage with dictionaries

If your pattern is a dictionary (object in js, for example: {cmd: "sum"}), you need to serialize it to a string before using it. Example:

pattern = json.dumps({"cmd": "sum"}) # '{"cmd": "sum"}'
await client.send(pattern, data)

Custom Transport

nest-rpc-client allows you to use your own custom transports. To do this, inherit from the Transport base class and implement its abstract methods:

from nest_rpc_client.transport import Transport

class MyCustomTransport(Transport):
    async def connect(self) -> None:
        # Initialize any connections or resources
        ...

    async def close(self) -> None:
        # Clean up resources or close connections
        ...

    async def send(self, pattern: str, data: dict) -> dict:
        # Implement the RPC request (expects a response)
        ...

    async def emit(self, pattern: str, data: dict) -> None:
        # Implement the fire-and-forget event
        ...

You can then use your custom transport exactly like the built-in ones:

transport = MyCustomTransport()
async with Client(transport) as client:
    result = await client.send("my_pattern", {"foo": "bar"})

Exception handling

nest-rpc-client raises RpcException whenever the RPC server returns an error. Example:

from nest_rpc_client.client import Client
from nest_rpc_client.transports.rabbitmq import RabbitMQTransport
from nest_rpc_client.config.rabbitmq import RabbitMQConfig
from nest_rpc_client.exceptions import RpcException

transport = RabbitMQTransport(RabbitMQConfig(url="amqp://guest:guest@localhost/", queue="rpc_queue"))

async with Client(transport) as client:
    try:
        result = await client.send("get_user", {"id": 123})
    except RpcException as e:
        print(f"RPC Error: {e.err}")

When the RPC response includes an err field (following the NestJS microservice pattern), the client automatically raises RpcException with the contents of err as its attribute:

{
    "id": "correlation-id",
    "err": {
        "message": "User not found",
        "code": 404
    }
}

This allows consistent error handling across all transports.

Tests:

This project includes both unit tests and full integration tests.

  • Unit tests are included in this repository and can be run with: pytest
  • Full integration tests are available in a separate repository: nest-rpc-client-tests

TODO:

  • Implement kafka transport
  • Implement mqtt transport

Metadata

Release files for nest-rpc-client 1.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nest-rpc-client 1.3.3
File Size Uploaded
nest_rpc_client-1.3.3.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nest-rpc-client 1.3.3
File Interpreter ABI Platform
nest_rpc_client-1.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 15.7 kB

Release files / nest_rpc_client-1.3.3.tar.gz

Download URL nest_rpc_client-1.3.3.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f97c5257b08d52f217ec43916cb35fc7c7f748c39ba4803cb9e3bc9f044b9e82
BLAKE2b-256 checksum
How to use checksums
af9f581ebfd21b3289693b66ead7dab7e91ab005017d99153f5b950eb671d4dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.11.2 Linux/6.1.0-31-amd64

Release files / nest_rpc_client-1.3.3-py3-none-any.whl

Download URL nest_rpc_client-1.3.3-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
236b45700ae07b089968bed4e8d858940d32f06865e83cd69840410903b801ed
BLAKE2b-256 checksum
How to use checksums
b3bcf0f234f20b2883e856001802bacad0e120dac4b853290fa4c97715b7a8ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.11.2 Linux/6.1.0-31-amd64
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page