Skip to content

NATS Connect Opts

This is not an official NATS project

This is a personal project and is not endorsed by the NATS.io community. It is not guaranteed to be maintained or supported.

This is an experimental project

This project is a prototype and should not be used for anything serious. It is not well tested, nor is it guaranteed to be correct.

The nats.go package (Go client for NATS) provides a simple way to configure connect options using the Options pattern

This project is an attempt to implement the same API in Python.

Note: This may not be Pythonic and may not be the best way to do it in Python. This is just an experiment.

References

How to install

pip install nats-connect-opts

Example usage

examples/minimal.py
from __future__ import annotations

from nats_contrib.connect_opts import connect, option


async def main() -> None:
    """A simple example of using the connect function."""

    client = await connect(
        # Configure the servers
        option.WithServers(
            [
                "nats://localhost:4222",
                "nats://localhost:4223",
            ]
        ),
        # Configure the reconnect strategy
        option.WithAllowReconnect(
            max_attempts=10,
            delay_seconds=0.5,
        ),
        # Configure the connection name
        option.WithConnectionName("my-connection"),
        # Configure the flusher
        option.WithFlusher(
            queue_size=100,
            timeout_seconds=10,
        ),
    )

    # Close the client
    await client.close()

Other works