Services
Module contents
This package implements the business logic and service layer of the application, separating it from request handling and data access.
- class back_chat.services.ConnectionManager[source]
Bases:
object
Manages active WebSocket connections for a FastAPI application.
This class provides methods to handle client connections and disconnections, send messages to individual clients, broadcast messages to all connected clients, and retrieve a list of currently connected users.
- async broadcast(message: str)[source]
Send a message to all currently connected clients.
Only clients with an active WebSocket connection (CONNECTED state) will receive the message. Any exceptions during sending will be logged to stdout.
- Parameters:
message – The text message to broadcast.
- async connect(client_id: str, websocket: WebSocket)[source]
Accept a new WebSocket connection and register it.
- Parameters:
client_id – Unique identifier for the connecting client.
websocket – The WebSocket connection object.
- disconnect(client_id: str)[source]
Remove a WebSocket connection from the active connections.
- Parameters:
client_id – The ID of the client to disconnect.
- class back_chat.services.RabbitMQManager(rabbitmq_url: str, manager, max_retries: int = 3, logger=None)[source]
Bases:
object
Asynchronous manager for interacting with RabbitMQ using aio-pika.
This class provides functionality to connect, publish, and consume messages from RabbitMQ queues or exchanges. It includes built-in retry logic and optional WebSocket broadcasting support for real-time notifications.
- Parameters:
rabbitmq_url – Connection URL for RabbitMQ.
manager – WebSocket manager instance used for broadcasting messages
to clients. :param max_retries: Maximum number of retry attempts for connecting to RabbitMQ. :param logger: Optional logger for debug and error logging.
- async connect() bool [source]
Attempt to establish a connection with RabbitMQ, retrying on failure.
- Returns:
True if the connection is successful, False otherwise.
- async consume_messages(queue_name: str)[source]
Consume messages from a RabbitMQ queue and broadcast them via WebSockets.
- Parameters:
queue_name – Name of the queue to consume from.
- async consume_messages_from_exchange(exchange_name: str)[source]
Consume messages from a RabbitMQ exchange and broadcast them via WebSockets.
- Parameters:
exchange_name – Name of the exchange to consume from.
Submodules
Connection_manager module
- class back_chat.services.connection_manager.ConnectionManager[source]
Bases:
object
Manages active WebSocket connections for a FastAPI application.
This class provides methods to handle client connections and disconnections, send messages to individual clients, broadcast messages to all connected clients, and retrieve a list of currently connected users.
- async broadcast(message: str)[source]
Send a message to all currently connected clients.
Only clients with an active WebSocket connection (CONNECTED state) will receive the message. Any exceptions during sending will be logged to stdout.
- Parameters:
message – The text message to broadcast.
- async connect(client_id: str, websocket: WebSocket)[source]
Accept a new WebSocket connection and register it.
- Parameters:
client_id – Unique identifier for the connecting client.
websocket – The WebSocket connection object.
- disconnect(client_id: str)[source]
Remove a WebSocket connection from the active connections.
- Parameters:
client_id – The ID of the client to disconnect.
Rabbitmq_manager module
- class back_chat.services.rabbitmq_manager.RabbitMQManager(rabbitmq_url: str, manager, max_retries: int = 3, logger=None)[source]
Bases:
object
Asynchronous manager for interacting with RabbitMQ using aio-pika.
This class provides functionality to connect, publish, and consume messages from RabbitMQ queues or exchanges. It includes built-in retry logic and optional WebSocket broadcasting support for real-time notifications.
- Parameters:
rabbitmq_url – Connection URL for RabbitMQ.
manager – WebSocket manager instance used for broadcasting messages
to clients. :param max_retries: Maximum number of retry attempts for connecting to RabbitMQ. :param logger: Optional logger for debug and error logging.
- async connect() bool [source]
Attempt to establish a connection with RabbitMQ, retrying on failure.
- Returns:
True if the connection is successful, False otherwise.
- async consume_messages(queue_name: str)[source]
Consume messages from a RabbitMQ queue and broadcast them via WebSockets.
- Parameters:
queue_name – Name of the queue to consume from.
- async consume_messages_from_exchange(exchange_name: str)[source]
Consume messages from a RabbitMQ exchange and broadcast them via WebSockets.
- Parameters:
exchange_name – Name of the exchange to consume from.