← Blog
Python

WebSockets en tiempo real con FastAPI y Redis Pub/Sub

⏱ 6 min de lectura 📅 12 de April de 2026 👁 48 vistas

Implementación completa de notificaciones en tiempo real usando FastAPI, Redis Pub/Sub y WebSockets nativos. El mismo sistema que usa FBPoster en producción.

WebSockets con FastAPI y Redis

En este artículo implementaremos notificaciones en tiempo real...

from fastapi import FastAPI, WebSocket
import redis.asyncio as redis

app = FastAPI()
r = redis.Redis(host="localhost", port=6379)

@app.websocket("/ws/{channel}")
async def websocket_endpoint(ws: WebSocket, channel: str):
    await ws.accept()
    pubsub = r.pubsub()
    await pubsub.subscribe(channel)
    async for message in pubsub.listen():
        if message["type"] == "message":
            await ws.send_text(message["data"])

Este patrón permite escalar horizontalmente sin perder mensajes.

#fastapi#websockets#redis#python#tiempo-real