A WebSocket endpoint in FastAPI is a special route that allows real-time, two-way communication between the client and server using the WebSocket protocol.
Click to reveal answer
beginner
How do you accept a WebSocket connection in FastAPI?
You accept a WebSocket connection by calling the await websocket.accept() method inside the WebSocket endpoint function.
Click to reveal answer
beginner
Which FastAPI class is used to handle WebSocket connections?
The <code>WebSocket</code> class from <code>fastapi</code> is used to handle WebSocket connections, allowing sending and receiving messages.
Click to reveal answer
beginner
What method do you use to receive a message from a WebSocket client in FastAPI?
Use await websocket.receive_text() to receive a text message from the client.
Click to reveal answer
beginner
How do you send a message back to the WebSocket client in FastAPI?
Use await websocket.send_text(message) to send a text message back to the client.
Click to reveal answer
Which decorator is used to create a WebSocket endpoint in FastAPI?
A@app.websocket
B@app.get
C@app.post
D@app.route
✗ Incorrect
The @app.websocket decorator defines a WebSocket endpoint in FastAPI.
What must you do first inside a WebSocket endpoint function to start communication?
ACall websocket.accept()
BCall websocket.close()
CCall websocket.send_text()
DCall websocket.receive_text()
✗ Incorrect
You must call await websocket.accept() to accept the connection before sending or receiving messages.
How do you receive a text message from the client in FastAPI WebSocket?
Aawait websocket.accept()
Bawait websocket.send_text()
Cawait websocket.close()
Dawait websocket.receive_text()
✗ Incorrect
Use await websocket.receive_text() to get a text message from the client.
Which protocol does FastAPI use for WebSocket communication?
AFTP
BHTTP/1.1
CWebSocket protocol
DSMTP
✗ Incorrect
FastAPI uses the WebSocket protocol for real-time two-way communication.
What happens if you don't call websocket.accept() in a FastAPI WebSocket endpoint?
AThe server crashes
BThe connection is not established
CMessages can still be sent and received
DThe connection closes immediately
✗ Incorrect
Without calling websocket.accept(), the WebSocket connection is not established and communication cannot start.
Explain step-by-step how to create a WebSocket endpoint in FastAPI that echoes messages back to the client.
Think about accepting connection, receiving, and sending messages.
You got /5 concepts.
Describe the role of the WebSocket class in FastAPI and how it helps in real-time communication.
Focus on what the WebSocket object lets you do inside the endpoint.
You got /4 concepts.
Practice
(1/5)
1. What decorator is used in FastAPI to create a WebSocket endpoint?
easy
A. @app.websocket
B. @app.get
C. @app.post
D. @app.route
Solution
Step 1: Understand FastAPI endpoint decorators
FastAPI uses different decorators for different endpoint types: @app.get for GET HTTP, @app.post for POST HTTP, and @app.websocket for WebSocket connections.
Step 2: Identify the correct decorator for WebSocket
Since WebSocket is a different protocol from HTTP, FastAPI uses @app.websocket to create WebSocket endpoints.
Hint: Use await with websocket.accept() to accept connection [OK]
Common Mistakes:
Forgetting to use await with async methods
Using websocket.connect() which does not exist
Calling accept() without await causing runtime errors
3. Given this FastAPI WebSocket endpoint code, what will it send back to the client when the client sends 'hello'?
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
medium
A. Message text was: hello
B. hello
C. Message text was: None
D. Error: receive_text() not awaited
Solution
Step 1: Trace the WebSocket message flow
The server accepts the connection, then waits to receive text from the client. It stores the received text in 'data'.
Step 2: Understand the response sent back
The server sends back a message with the text 'Message text was: ' plus the received data. If client sends 'hello', the response is 'Message text was: hello'.
Final Answer:
Message text was: hello -> Option A
Quick Check:
Sent message = 'Message text was: hello' [OK]
Hint: Server echoes with prefix 'Message text was:' [OK]
Common Mistakes:
Confusing received message with sent message
Missing await causing runtime errors
Assuming server sends back raw client message without prefix
4. Identify the error in this FastAPI WebSocket endpoint code:
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
websocket.accept()
data = await websocket.receive_text()
await websocket.send_text(data)
medium
A. Missing await before websocket.receive_text()
B. Missing await before websocket.accept()
C. Missing await before websocket.send_text()
D. Incorrect WebSocket path
Solution
Step 1: Check async method calls
In FastAPI, websocket.accept() is an async method and must be awaited.
Step 2: Identify missing await
The code calls websocket.accept() without await, which will cause a runtime error.
Final Answer:
Missing await before websocket.accept() -> Option B
Quick Check:
Async methods require await = missing await [OK]
Hint: Always await async WebSocket methods like accept() [OK]
Common Mistakes:
Calling async methods without await
Confusing which methods are async
Assuming accept() is synchronous
5. You want to create a FastAPI WebSocket endpoint that continuously receives messages and sends back each message reversed. Which code snippet correctly implements this behavior?
hard
A. async def websocket_endpoint(websocket: WebSocket):
websocket.accept()
while True:
data = websocket.receive_text()
websocket.send_text(data[::-1])
B. async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
data = await websocket.receive_text()
await websocket.send_text(data[::-1])
C. async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
for data in websocket.receive_text():
await websocket.send_text(data[::-1])
D. async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(data[::-1])
Solution
Step 1: Understand continuous message handling
To handle multiple messages, the server must loop indefinitely, receiving and sending messages repeatedly.
Step 2: Check async calls and loop structure
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(data[::-1]) uses an async while True loop, awaits receive_text(), and sends back reversed text with await send_text(), correctly handling async and continuous flow.
Final Answer:
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(data[::-1]) -> Option D
Quick Check:
Continuous async loop with await calls = async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(data[::-1]) [OK]
Hint: Use while True with await receive and send for continuous WebSocket [OK]
Common Mistakes:
Missing loop for continuous messages
Not awaiting async methods
Using for loop incorrectly on async receive_text()
Calling async methods without await
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.