Complete the code to import the FastAPI class.
from fastapi import [1] app = [1]()
The FastAPI class is imported to create the app instance.
Complete the code to accept a WebSocket connection in FastAPI.
@app.websocket("/ws") async def websocket_endpoint(websocket: [1]): await websocket.accept()
The WebSocket parameter type is used to handle WebSocket connections.
Fix the error in the code to send a message to the WebSocket client.
await websocket.[1](text_data="Hello Client")
The correct method to send text data is send with the text_data argument.
Fill both blanks to broadcast a message to all connected clients stored in a list.
clients = [] async def broadcast(message): for client in clients: await client.[1](text_data=[2])
To send a message to each client, use send with the message variable.
Fill all three blanks to add a new WebSocket client to the list and broadcast a welcome message.
clients = [] @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() clients.[1](websocket) await broadcast([2]) async def broadcast(message): for client in clients: await client.send(text_data=[3])
Use append to add the client, send a welcome string, and broadcast the message variable.
