Complete the code to import the WebSocket class from FastAPI.
from fastapi import [1]
The WebSocket class is imported from FastAPI to handle WebSocket connections.
Complete the code to accept a WebSocket connection inside an endpoint.
async def websocket_endpoint(websocket: WebSocket): await websocket.[1]()
The accept() method is called to accept the WebSocket connection from the client.
Fix the error in the code to receive a message from the client.
data = await websocket.[1]()The receive_text() method receives a text message from the client over the WebSocket.
Fill both blanks to send a message back to the client with the received data.
await websocket.[1](text='Message received: ' + [2])
Use send_text to send a message, and data is the variable holding the received message.
Fill all three blanks to create a simple FastAPI WebSocket app that echoes messages.
from fastapi import FastAPI, [1] app = FastAPI() @app.websocket('/ws') async def websocket_endpoint(websocket: [2]): await websocket.[3]() while True: data = await websocket.receive_text() await websocket.send_text(f'Echo: {data}')
Import WebSocket, use it as the parameter type, and call accept() to start the connection.