tezvyn:

How do you define a WebSocket endpoint in FastAPI?

Source: fastapi.tiangolo.combeginner

WHAT IT TESTS: async endpoint wiring and the accept-receive-send lifecycle. ANSWER OUTLINE: import WebSocket, use @app.websocket, await accept, receive_text, then send_text. RED FLAG: forgetting accept or treating it like a standard HTTP route.

WHAT IT TESTS: whether you understand FastAPI's async WebSocket lifecycle and can distinguish it from standard HTTP request-response routes. ANSWER OUTLINE: import WebSocket from fastapi; decorate a function with @app.websocket; await websocket.accept to complete the handshake; await websocket.receive_text to read the client's message; then await websocket.send_text to return a response. RED FLAG: forgetting to call accept, using a Request parameter instead of WebSocket, or omitting await on the async I/O methods.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How do you define a WebSocket endpoint in FastAPI? · Tezvyn