tutorial003_py39.py — fastapi Source File
Architecture documentation for tutorial003_py39.py, a python file in the fastapi codebase. 2 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 1cc0df18_a262_3520_422c_6ce4f27e3b54["tutorial003_py39.py"] 534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"] 1cc0df18_a262_3520_422c_6ce4f27e3b54 --> 534f6e44_61b8_3c38_8b89_6934a6df9802 967b6712_70e2_f5fa_f671_7c149857a445["responses.py"] 1cc0df18_a262_3520_422c_6ce4f27e3b54 --> 967b6712_70e2_f5fa_f671_7c149857a445 style 1cc0df18_a262_3520_422c_6ce4f27e3b54 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
app = FastAPI()
html = """
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
</head>
<body>
<h1>WebSocket Chat</h1>
<h2>Your ID: <span id="ws-id"></span></h2>
<form action="" onsubmit="sendMessage(event)">
<input type="text" id="messageText" autocomplete="off"/>
<button>Send</button>
</form>
<ul id='messages'>
</ul>
<script>
var client_id = Date.now()
document.querySelector("#ws-id").textContent = client_id;
var ws = new WebSocket(`ws://localhost:8000/ws/${client_id}`);
ws.onmessage = function(event) {
var messages = document.getElementById('messages')
var message = document.createElement('li')
var content = document.createTextNode(event.data)
message.appendChild(content)
messages.appendChild(message)
};
function sendMessage(event) {
var input = document.getElementById("messageText")
ws.send(input.value)
input.value = ''
event.preventDefault()
}
</script>
</body>
</html>
"""
class ConnectionManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def send_personal_message(self, message: str, websocket: WebSocket):
await websocket.send_text(message)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
@app.get("/")
async def get():
return HTMLResponse(html)
@app.websocket("/ws/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: int):
await manager.connect(websocket)
try:
while True:
data = await websocket.receive_text()
await manager.send_personal_message(f"You wrote: {data}", websocket)
await manager.broadcast(f"Client #{client_id} says: {data}")
except WebSocketDisconnect:
manager.disconnect(websocket)
await manager.broadcast(f"Client #{client_id} left the chat")
Domain
Subdomains
Functions
Classes
Dependencies
Source
Frequently Asked Questions
What does tutorial003_py39.py do?
tutorial003_py39.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Routing subdomain.
What functions are defined in tutorial003_py39.py?
tutorial003_py39.py defines 2 function(s): get, websocket_endpoint.
What does tutorial003_py39.py depend on?
tutorial003_py39.py imports 2 module(s): __init__.py, responses.py.
Where is tutorial003_py39.py in the architecture?
tutorial003_py39.py is located at docs_src/websockets/tutorial003_py39.py (domain: FastAPI, subdomain: Routing, directory: docs_src/websockets).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free