Socket.IO wrapper for FastApi applications
Project description
fastapi-socketio-handler
FastAPI + Socket.IO integration made modular, extensible and simple.
A clean event-based wrapper that helps you organize your Socket.IO server logic using decorators, handlers, and namespaces — powered by python-socketio, FastAPI, and asyncio.
🔧 Features
- 📡 Socket.IO server for FastAPI apps
- 🧩 Handler registration via decorators
- 📁 Namespace-based routing
- 🔁 Redis pub/sub support (scaling)
- 💡 Typed, extensible, and testable architecture
- 🧪 Ready for pytest & async testing
📦 Installation
pip install fastapi-socketio-handler
🚀 Quick Start
1. Define a handler
# app/chat_handler.py
from fastapi_socketio_handler import BaseSocketHandler, register_handler
@register_handler(namespace="/chat")
class ChatSocketHandlers(BaseSocketHandler):
def register_events(self):
self.sio.on("typing", self.event_typing, namespace=self.namespace)
self.sio.on("stop_typing", self.event_stop_typing, namespace=self.namespace)
async def connect(self, sid: str, environ: dict, auth: dict = None):
if not auth or "token" not in auth:
return False # Reject connection
return True
async def event_typing(self, sid: str, data: dict):
print(f"Typing: {data}")
async def event_stop_typing(self, sid: str, data: dict):
print(f"Stopped typing: {data}")
2. Use with lifespan (recommended)
# lifespan.py
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi_socketio_handler import get_socket_manager
import app.chat_handler # 👈 force-import handlers to trigger decorator registration
@asynccontextmanager
async def lifespan(app: FastAPI):
socket_manager = get_socket_manager(
redis_url="redis://localhost:6379", # Optional Redis
async_session=your_async_session_factory, # Optional DB session factory
)
socket_manager.mount_to_app(app)
socket_manager.register_events()
async with socket_manager:
yield
3. Connect from frontend
const socket = io('http://localhost:8000/chat', {
auth: {
token: 'your-auth-token'
}
});
socket.emit("typing", { chatId: "..." });
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_socketio_handler-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_socketio_handler-0.1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09a97755e9abc97ad0e9eb12c515a42bfebc6d89ac856aadecd9047d09b8dc16
|
|
| MD5 |
03e6e1acceecd21935c5cc85efcba527
|
|
| BLAKE2b-256 |
da51098d0dd318da17bfa0076cd6c662f0e5a775b14c71abb6c37bc0e30ab274
|
File details
Details for the file fastapi_socketio_handler-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_socketio_handler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ced05a4ea614b2dae112a9efa6e5e986ab76d5ade74829907d8511e4eed97ef7
|
|
| MD5 |
e995bcdceba813b1a292371190d18b4e
|
|
| BLAKE2b-256 |
b6a7aee1b08cb3d630613c1050a84283c7f83a1a6718808f4b581e01a42045c8
|