Complete the code to import the WebSocket class from FastAPI.
from fastapi import [1]
The WebSocket class is imported from FastAPI to create WebSocket endpoints.
Complete the code to define a WebSocket endpoint path '/ws'.
@app.websocket("[1]") def websocket_endpoint(websocket: WebSocket): await websocket.accept()
The WebSocket endpoint is defined at path /ws using the @app.websocket decorator.
Fix the error in the code to accept the WebSocket connection properly.
async def websocket_endpoint(websocket: WebSocket): await websocket.[1]()
The method accept() is used to accept an incoming WebSocket connection.
Fill both blanks to receive a text message and send it back to the client.
data = await websocket.[1]() await websocket.[2](data)
Use receive_text() to get a text message and send_text() to send it back.
Fill all three blanks to create a complete WebSocket echo endpoint that accepts, receives, sends, and closes the connection.
from fastapi import FastAPI, WebSocket app = FastAPI() @app.websocket("/echo") async def echo_endpoint(websocket: WebSocket): await websocket.[1]() data = await websocket.[2]() await websocket.[3](data) await websocket.close()
This code accepts the WebSocket connection, receives a text message, sends it back, and then closes the connection.