Consider a FastAPI WebSocket endpoint that uses a single broadcaster instance to send messages to all connected clients. What is the expected behavior when three clients connect and one client sends a message?
from fastapi import FastAPI, WebSocket, WebSocketDisconnect from fastapi_broadcast import Broadcast app = FastAPI() broadcaster = Broadcast("memory://") @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() await broadcaster.connect(websocket) try: while True: data = await websocket.receive_text() await broadcaster.send(data) except WebSocketDisconnect: await broadcaster.disconnect(websocket)
Think about how a broadcaster is designed to distribute messages.
A broadcaster in FastAPI is designed to send messages to all connected clients. When one client sends a message, the broadcaster distributes it to every client connected to that broadcaster, enabling real-time communication among multiple clients.
Choose the correct way to create a Broadcast instance that uses in-memory transport for message broadcasting.
Check the URI scheme for in-memory transport.
The correct URI scheme for in-memory broadcasting in FastAPI Broadcast is "memory://". This creates a broadcaster that keeps messages in memory without external dependencies.
Examine the code below. It raises a RuntimeError: 'Task attached to a different loop'. What is the cause?
from fastapi import FastAPI, WebSocket from fastapi_broadcast import Broadcast import asyncio app = FastAPI() broadcaster = Broadcast("memory://") @app.on_event("startup") async def startup_event(): await broadcaster.connect() @app.on_event("shutdown") async def shutdown_event(): await broadcaster.disconnect() @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() await broadcaster.connect(websocket) try: while True: data = await websocket.receive_text() await broadcaster.send(data) except Exception as e: print(e)
Consider how asyncio event loops work with tasks and connections.
The RuntimeError occurs because the broadcaster was connected during the startup event loop, but WebSocket connections run in a different event loop. Tasks cannot be shared across different event loops, causing this error.
Given the code below, what is the value of connected_clients after three clients connect and then one disconnects?
from fastapi import FastAPI, WebSocket from fastapi_broadcast import Broadcast app = FastAPI() broadcaster = Broadcast("memory://") connected_clients = set() @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() connected_clients.add(websocket) try: while True: data = await websocket.receive_text() await broadcaster.send(data) except Exception: connected_clients.remove(websocket)
Think about how the set changes when clients connect and disconnect.
Initially, three clients connect, so connected_clients has 3 items. When one disconnects, it is removed from the set, leaving 2 clients connected.
Choose the statement that correctly explains how the Broadcast instance facilitates communication among multiple WebSocket clients in FastAPI.
Focus on the broadcasting role rather than storage or security features.
The Broadcast instance in FastAPI acts as a central hub that receives messages from any connected client and forwards them to all others, enabling real-time group communication. It does not store messages permanently, encrypt messages, or limit client connections by itself.