askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/dbbasic_websocket.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
dbbasic_websocket.py · 3.1 KB · 93 lines
1#!/usr/bin/env python3
2"""
3DBBasic WebSocket Handler
4Unified WebSocket support for live UI updates
5"""
6
7from fastapi import WebSocket, WebSocketDisconnect
8from typing import List, Dict
9import json
10import asyncio
11
12class ConnectionManager:
13 """Manage WebSocket connections"""
14
15 def __init__(self):
16 self.active_connections: List[WebSocket] = []
17 self.service_connections: Dict[str, List[WebSocket]] = {}
18
19 async def connect(self, websocket: WebSocket, service: str = None):
20 """Accept new connection"""
21 await websocket.accept()
22 self.active_connections.append(websocket)
23
24 if service:
25 if service not in self.service_connections:
26 self.service_connections[service] = []
27 self.service_connections[service].append(websocket)
28
29 def disconnect(self, websocket: WebSocket, service: str = None):
30 """Remove connection"""
31 self.active_connections.remove(websocket)
32
33 if service and service in self.service_connections:
34 self.service_connections[service].remove(websocket)
35
36 async def send_personal_message(self, message: str, websocket: WebSocket):
37 """Send message to specific connection"""
38 await websocket.send_text(message)
39
40 async def broadcast(self, message: str, service: str = None):
41 """Broadcast to all or service-specific connections"""
42 connections = self.service_connections.get(service, self.active_connections) if service else self.active_connections
43
44 for connection in connections:
45 try:
46 await connection.send_text(message)
47 except:
48 # Connection closed, remove it
49 await self.disconnect(connection, service)
50
51# Global connection manager
52manager = ConnectionManager()
53
54async def websocket_endpoint(websocket: WebSocket, service: str = None):
55 """WebSocket endpoint for real-time updates"""
56 await manager.connect(websocket, service)
57
58 try:
59 while True:
60 # Wait for messages from client
61 data = await websocket.receive_text()
62
63 # Process message
64 message = json.loads(data)
65
66 if message.get('type') == 'subscribe':
67 # Subscribe to specific events
68 pass
69 elif message.get('type') == 'update':
70 # Broadcast UI update
71 await manager.broadcast(json.dumps({
72 'type': 'ui_update',
73 'target': message.get('target'),
74 'data': message.get('data')
75 }), service)
76
77 except WebSocketDisconnect:
78 manager.disconnect(websocket, service)
79
80async def send_ui_update(target_id: str, new_value: any, service: str = None):
81 """Send UI update to all connected clients"""
82 await manager.broadcast(json.dumps({
83 'type': 'ui_update',
84 'target': target_id,
85 'value': new_value
86 }), service)
87
88async def send_metric_update(metrics: Dict, service: str = None):
89 """Send metric updates"""
90 await manager.broadcast(json.dumps({
91 'type': 'metrics',
92 'data': metrics
93 }), service)