FastAPI is a powerhouse when it comes to building high-performance web APIs, but did you know you can take it to the next level with WebSockets? Let’s dive into how you can optimize your FastAPI applications using WebSockets for real-time communication.
First things first, what are WebSockets? Think of them as a direct phone line between your server and client. Unlike traditional HTTP requests, WebSockets allow for full-duplex communication, meaning both sides can send messages to each other at any time. This is perfect for real-time applications like chat systems, live updates, or gaming.
To get started with WebSockets in FastAPI, you’ll need to install the websockets
library. You can do this easily with pip:
pip install websockets
Now, let’s create a simple WebSocket endpoint in FastAPI:
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
This code sets up a WebSocket endpoint at “/ws”. When a client connects, it accepts the connection and then enters a loop where it waits for messages, echoing them back to the client.
But wait, there’s more! WebSockets really shine when you need to broadcast messages to multiple clients. Let’s create a simple chat room:
from fastapi import FastAPI, WebSocket
from typing import List
app = FastAPI()
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
@app.websocket("/ws/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: int):
await manager.connect(websocket)
try:
while True:
data = await websocket.receive_text()
await manager.broadcast(f"Client #{client_id} says: {data}")
except WebSocketDisconnect:
manager.disconnect(websocket)
await manager.broadcast(f"Client #{client_id} left the chat")
This code creates a ConnectionManager
class to handle multiple WebSocket connections. When a client connects, it’s added to the list of active connections. When a client sends a message, it’s broadcasted to all connected clients.
Now, you might be thinking, “This is cool, but how does it optimize performance?” Well, WebSockets are incredibly efficient for real-time communication. Instead of constantly polling the server for updates, clients maintain an open connection and receive updates instantly. This reduces server load and network traffic, especially for applications that require frequent updates.
But hold on, we’re not done yet! Let’s talk about scaling WebSockets. As your application grows, you might need to handle thousands or even millions of concurrent WebSocket connections. This is where things get really interesting.
One approach to scaling WebSockets is to use a message queue like Redis. Here’s how you could modify our chat room example to use Redis:
import aioredis
from fastapi import FastAPI, WebSocket
from typing import List
app = FastAPI()
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
self.redis = None
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def broadcast(self, message: str):
if not self.redis:
self.redis = await aioredis.create_redis_pool("redis://localhost")
await self.redis.publish("chat", message)
async def redis_listener(self):
if not self.redis:
self.redis = await aioredis.create_redis_pool("redis://localhost")
channel = (await self.redis.subscribe("chat"))[0]
while True:
message = await channel.get()
for connection in self.active_connections:
await connection.send_text(message.decode())
manager = ConnectionManager()
@app.on_event("startup")
async def startup_event():
asyncio.create_task(manager.redis_listener())
@app.websocket("/ws/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: int):
await manager.connect(websocket)
try:
while True:
data = await websocket.receive_text()
await manager.broadcast(f"Client #{client_id} says: {data}")
except WebSocketDisconnect:
manager.disconnect(websocket)
await manager.broadcast(f"Client #{client_id} left the chat")
In this version, we’re using Redis as a pub/sub system. When a message is broadcast, it’s published to a Redis channel. A separate task listens to this channel and sends the messages to all connected WebSockets. This approach allows you to scale your WebSocket server across multiple processes or even multiple machines.
But what if you need even more performance? Enter asyncio. FastAPI is built on top of Starlette, which uses asyncio for asynchronous programming. This means you can handle a large number of WebSocket connections concurrently without blocking.
Here’s an example of how you could use asyncio to handle multiple tasks alongside your WebSocket connections:
import asyncio
from fastapi import FastAPI, WebSocket
app = FastAPI()
async def background_task():
while True:
await asyncio.sleep(60)
print("Performing background task...")
@app.on_event("startup")
async def startup_event():
asyncio.create_task(background_task())
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
await websocket.send_text(f"You said: {data}")
except WebSocketDisconnect:
print("Client disconnected")
In this example, we’re running a background task alongside our WebSocket endpoint. This task runs every 60 seconds, but it doesn’t block the WebSocket connections because it’s running asynchronously.
Now, let’s talk about security. When working with WebSockets, it’s crucial to implement proper authentication and authorization. FastAPI makes this easy with its dependency injection system. Here’s an example of how you could secure your WebSocket endpoint:
from fastapi import FastAPI, WebSocket, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
async def get_current_user(token: str = Depends(oauth2_scheme)):
user = await database_lookup(token)
if not user:
raise HTTPException(status_code=401, detail="Invalid token")
return user
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket, current_user: dict = Depends(get_current_user)):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
In this example, we’re using OAuth2 for authentication. The get_current_user
function checks the token and returns the user if it’s valid. If the token is invalid, it raises an exception, which will prevent the WebSocket connection from being established.
One more thing to consider when optimizing WebSocket performance is compression. WebSockets support per-message deflate compression, which can significantly reduce the amount of data transferred, especially for text-heavy applications. Here’s how you can enable compression in FastAPI:
from fastapi import FastAPI, WebSocket
from starlette.websockets import WebSocketState
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept(subprotocol="deflate")
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
By specifying the “deflate” subprotocol, we’re telling the client that we support compression. The client can then choose to use compression if it supports it.
Remember, while WebSockets are powerful, they’re not always the best solution. For simple applications that don’t require real-time updates, traditional HTTP requests might be simpler and more appropriate. Always consider your specific use case when deciding whether to use WebSockets.
In conclusion, WebSockets in FastAPI offer a powerful way to implement real-time communication in your applications. By leveraging asyncio, proper scaling techniques, and security measures, you can create high-performance, real-time applications that can handle a large number of concurrent connections. Whether you’re building a chat application, a live dashboard, or a multiplayer game, WebSockets in FastAPI provide the tools you need to succeed. So go ahead, give it a try, and watch your applications come alive with real-time capabilities!