0
0
FastAPIframework~20 mins

WebSocket endpoint creation in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a client sends 'hello' to this WebSocket endpoint?
Consider this FastAPI WebSocket endpoint code. What will the client receive after sending the message 'hello'?
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}')
    await websocket.close()
ARuntimeError due to missing await
B"hello"
C"Message received: hello"
DConnection closed without response
Attempts:
2 left
💡 Hint
Look at what the server sends back after receiving the message.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a FastAPI WebSocket endpoint?
Select the option that correctly defines a WebSocket endpoint in FastAPI without syntax errors.
A
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket('/chat')
async def chat(websocket: WebSocket):
    await websocket.accept()
    await websocket.send_text('Welcome!')
B
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket('/chat')
def chat(websocket: WebSocket):
    websocket.accept()
    websocket.send_text('Welcome!')
C
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket('/chat')
async def chat():
    await websocket.accept()
    await websocket.send_text('Welcome!')
D
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket('/chat')
async def chat(websocket):
    await websocket.accept()
    await websocket.send_text('Welcome!')
Attempts:
2 left
💡 Hint
Check for async keyword and parameter type.
state_output
advanced
2:00remaining
What is the value of 'count' after 3 messages are received in this WebSocket endpoint?
Given this WebSocket endpoint, what will be the value of 'count' after the client sends 3 messages?
FastAPI
from fastapi import FastAPI, WebSocket

app = FastAPI()

count = 0

@app.websocket('/counter')
async def counter(websocket: WebSocket):
    global count
    await websocket.accept()
    for _ in range(3):
        await websocket.receive_text()
        count += 1
    await websocket.send_text(f'Count is {count}')
    await websocket.close()
ARaises UnboundLocalError
B0
C1
D3
Attempts:
2 left
💡 Hint
Look at how 'count' is updated inside the loop.
🔧 Debug
advanced
2:00remaining
Which option correctly describes the behavior when connecting to this WebSocket endpoint?
Identify which option correctly describes the behavior when a client connects to this WebSocket endpoint.
FastAPI
from fastapi import FastAPI, WebSocket

app = FastAPI()

@app.websocket('/echo')
async def echo(websocket: WebSocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        await websocket.send_text(data)
AClient connects but server never accepts connection.
BClient sends 'test' and receives 'test' back repeatedly.
CClient connects and server closes connection immediately.
DClient connects and server raises RuntimeError due to missing await.
Attempts:
2 left
💡 Hint
Check if the server accepts connection and awaits properly.
🧠 Conceptual
expert
2:00remaining
What happens if you omit 'await websocket.accept()' in a FastAPI WebSocket endpoint?
In FastAPI, what is the effect of not calling 'await websocket.accept()' inside a WebSocket endpoint function?
AThe client connection is rejected and the handshake fails.
BThe server accepts the connection automatically without explicit accept call.
CThe server sends a default welcome message to the client.
DThe server raises a SyntaxError at runtime.
Attempts:
2 left
💡 Hint
Think about the WebSocket handshake process.