0
0
FastAPIframework~20 mins

Why WebSockets enable real-time communication in FastAPI - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Real-Time Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How do WebSockets maintain a connection?

In FastAPI, WebSockets allow continuous communication between client and server. How do WebSockets keep this connection open?

AThey use HTTP polling to check for new messages every few seconds.
BThey repeatedly open and close HTTP connections for each message sent.
CThey send messages only when the client refreshes the page.
DThey open a single TCP connection that stays open for ongoing message exchange.
Attempts:
2 left
💡 Hint

Think about a phone call that stays connected instead of hanging up after each sentence.

component_behavior
intermediate
2:00remaining
What happens when a FastAPI WebSocket receives a message?

Consider a FastAPI WebSocket endpoint that echoes messages back to the client. What is the expected behavior when the client sends a message?

FastAPI
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 received: {data}')
AThe server closes the connection immediately after receiving the message.
BThe server accepts the connection, receives the message, and sends back a confirmation with the message content.
CThe server ignores the message and sends no response.
DThe server crashes because it does not handle binary messages.
Attempts:
2 left
💡 Hint

Look at the send_text line and what it sends back.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this FastAPI WebSocket code

Which option contains the correct syntax for accepting a WebSocket connection and receiving a text message?

FastAPI
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'Received: {data}')
A
def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    data = await websocket.receive_text()
    await websocket.send_text(f'Received: {data}')
B
async def websocket_endpoint(websocket: WebSocket):
    websocket.accept()
    data = websocket.receive_text()
    websocket.send_text(f'Received: {data}')
C
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    data = await websocket.receive_text()
    await websocket.send_text(f'Received: {data}')
D
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept
    data = await websocket.receive_text()
    await websocket.send_text(f'Received: {data}')
Attempts:
2 left
💡 Hint

Check for missing await keywords and function definitions.

state_output
advanced
2:00remaining
What is the output after multiple WebSocket messages?

Given this FastAPI WebSocket code that counts messages, what is the value of count after receiving three messages?

FastAPI
from fastapi import FastAPI, WebSocket
app = FastAPI()

@app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    count = 0
    for _ in range(3):
        await websocket.receive_text()
        count += 1
    await websocket.send_text(f'Messages received: {count}')
A'Messages received: 3'
B'Messages received: 0'
C'Messages received: 1'
DRuntimeError due to missing await in loop
Attempts:
2 left
💡 Hint

Count increments inside the loop for each message received.

🔧 Debug
expert
2:00remaining
Why does this FastAPI WebSocket code raise an error?

Examine this WebSocket endpoint code. Why does it raise a RuntimeError: Cannot call receive_text() after connection is closed?

FastAPI
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.close()
    data2 = await websocket.receive_text()
ABecause the code tries to receive a message after closing the WebSocket connection.
BBecause the WebSocket was never accepted before receiving messages.
CBecause receive_text() requires a timeout parameter.
DBecause the close() method is not awaited properly.
Attempts:
2 left
💡 Hint

Think about what happens after closing a connection.