fras-backend / services /notifier.py
pranaykumar2005's picture
Update for Hugging Face
491a827
Raw
History Blame Contribute Delete
1.08 kB
from fastapi import WebSocket
from typing import List
import logging
logger = logging.getLogger(__name__)
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
logger.info(f"WebSocket connected. Total: {len(self.active_connections)}")
def disconnect(self, websocket: WebSocket):
if websocket in self.active_connections:
self.active_connections.remove(websocket)
logger.info(f"WebSocket disconnected. Total: {len(self.active_connections)}")
async def broadcast(self, message: dict):
# Broadcast JSON message to all connected clients
for connection in list(self.active_connections):
try:
await connection.send_json(message)
except Exception as e:
logger.warning(f"Error sending to websocket: {e}")
self.disconnect(connection)
manager = ConnectionManager()