Recommended Scaleable Web-sockets Libraries in 2026 #14807
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse
app = FastAPI()
html = """
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
</head>
<body>
<h1>WebSocket Chat</h1>
<form action="" onsubmit="sendMessage(event)">
<input type="text" id="messageText" autocomplete="off"/>
<button>Send</button>
</form>
<ul id='messages'>
</ul>
<script>
var ws = new WebSocket("ws://localhost:8000/ws");
ws.onmessage = function(event) {
var messages = document.getElementById('messages')
var message = document.createElement('li')
var content = document.createTextNode(event.data)
message.appendChild(content)
messages.appendChild(message)
};
function sendMessage(event) {
var input = document.getElementById("messageText")
ws.send(input.value)
input.value = ''
event.preventDefault()
}
</script>
</body>
</html>
"""
@app.get("/")
async def get():
return HTMLResponse(html)
# TODO FIXME scale horizontally
@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}")Description
Operating SystemLinux Operating System DetailsNo response FastAPI VersionLatest Pydantic Version2.* Python Version3.14 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
One with a similar name, but that is still active: https://github.com/pyropy/fastapi-socketio |
Beta Was this translation helpful? Give feedback.
-
|
Since Recommended Solutions1. python-socketio (Most Popular)pip install python-socketioimport socketio
from fastapi import FastAPI
sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*')
app = FastAPI()
socket_app = socketio.ASGIApp(sio, app)
@sio.event
async def connect(sid, environ):
print(f"Client connected: {sid}")
@sio.event
async def message(sid, data):
await sio.emit('response', {'data': data}, room=sid)For horizontal scaling, use Redis adapter: import socketio
mgr = socketio.AsyncRedisManager('redis://localhost:6379')
sio = socketio.AsyncServer(client_manager=mgr, async_mode='asgi')2. Redis Pub/Sub Directly (Simplest)import redis.asyncio as redis
from fastapi import WebSocket
redis_client = redis.from_url("redis://localhost:6379")
@app.websocket("/ws/{channel}")
async def websocket_endpoint(websocket: WebSocket, channel: str):
await websocket.accept()
pubsub = redis_client.pubsub()
await pubsub.subscribe(channel)
async for message in pubsub.listen():
if message["type"] == "message":
await websocket.send_text(message["data"].decode())3. Centrifugo (Production-Grade)Standalone server, connects via HTTP API:
GitHub: https://github.com/centrifugal/centrifugo Comparison
My recommendation: Start with |
Beta Was this translation helpful? Give feedback.
Since
broadcasterandfastapi-socketioare archived, here are the actively maintained options in 2026:Recommended Solutions
1. python-socketio (Most Popular)
For horizontal scaling, use Redis adapter: