Skip to main content

Pydantic-enhanced SocketIO with FastAPI integration support.

Project description

Pydantic-SocketIO

GitHub PyPI

A Pydantic-enhanced SocketIO library for Python, with FastAPI integration.

Features

⭐️ Pydantic-Enhanced SocketIO: Drop-in replacements for the original python-socketio server and client (sync and async), with built-in Pydantic validation for event data. You can also easily monkey patch this validation to the original socketio server and client.

🪐 Easy Integration with FastAPI: Seamlessly integrates Socket.IO with FastAPI, allowing you to manage event-driven communication effortlessly.

Installation

pip install pydantic-socketio

If you want FastAPI integration, you can install the extra dependencies:

pip install pydantic-socketio[fastapi]

Other options of original python-socketio are also available: client, asyncio-client, docs.

Usage

Recommended: Pydantic-Enhanced SocketIO Server and Client

Drop-in replacements for the original python-socketio server and client are provided.

The enhanced SocketIO server with Pydantic validation:

from pydantic import BaseModel
import pydantic_socketio

class ChatMessage(BaseModel):
    role: str
    content: str

# Create an enhanced SocketIO server; use AsyncServer for async server
sio = pydantic_socketio.Server()

# Define an event with Pydantic validation
@sio.event
def message(data: ChatMessage):
    print(f"Received chat message from {data.role}: {data.content}")
    data.content = data.content.upper()
    print(f"Sending uppercase message: {data.content}")
    # Emit an event with Pydantic model without any additional conversion
    sio.emit("message", data)

# `on` decorator is also supported
@sio.on("custom_event")
def handle_custom_event(data: int):
    ...

The enhanced SocketIO client with Pydantic validation:

import pydantic_socketio

# Create an enhanced SocketIO client; use AsyncClient for async client
sio = pydantic_socketio.Client()

@sio.event
def ping(data: int):
    ...

@sio.on("pong")
def handle_pong(data: int):
    ...

Alternative: Monkey Patching for Original SocketIO

Alternatively, if you want to apply Pydantic validation to the original python-socketio server and client without replacing them, you can use the monkey_patch() method:

from pydantic_socketio import monkey_patch
import socketio

# Apply monkey patch to the original socketio server and client
monkey_patch()

# Now, you can use the original socketio server and client with Pydantic validation
sio = socketio.Server()

@sio.event
def ping(data: int):
    print(f"Received ping: {data}")
    data += 1
    print(f"Sending pong: {data}")
    sio.emit("poing", data)

FastAPI Integration

You can easily integrate the enhanced socketio server with FastAPI by using FastAPISocketIO:

from fastapi import FastAPI
from pydantic_socketio import FastAPISocketIO

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}
...

# Create a FastAPI socketio server
sio = FastAPISocketIO(app)

@sio.event
async def ping(data: int):
    print(f"Received ping: {data}")
    data += 1
    print(f"Sending pong: {data}")
    await sio.emit("pong", data)

# Both sync and async event handlers are supported, as per the original python-socketio
@sio.on("custom_event")
def handle_custom_event(data: int):
    ...

You can also integrate the SocketIO server manually after FastAPI initialization:

from fastapi import FastAPI
from pydantic_socketio import FastAPISocketIO

sio = FastAPISocketIO()
...
app = FastAPI()
...

# Integrate the SocketIO server to FastAPI
sio.integrate(app)

FastAPI Dependency Injection

You can use SioDep as a FastAPISocketIO dependency injection in FastAPI applications:

from fastapi import FastAPI
from pydantic_socketio import FastAPISocketIO, SioDep

app = FastAPI()
sio = FastAPISocketIO(app)

# You may define this endpoint in another file, like in a separate router
@app.get("/")
async def root(sio: SioDep):
    await sio.emit("message", "API root called")
    return {"Hello": "World"}

Original Documentation

More details can be found in the original python-socketio documentation.

License

Pydantic-SocketIO © 2025 by Atomie CHEN is licensed under the MIT License.

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

pydantic_socketio-0.1.3.tar.gz (200.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_socketio-0.1.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_socketio-0.1.3.tar.gz.

File metadata

  • Download URL: pydantic_socketio-0.1.3.tar.gz
  • Upload date:
  • Size: 200.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pydantic_socketio-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b030a54eb2949db9c330ff5c6619886b5f13825c766de3ce5fcdb6f0709e37e4
MD5 c85606a5d01ce97ec043b119f1b67e92
BLAKE2b-256 91d779fcfd852c1bd9a7ef055faff5a0f5e2dcb7097f1aac33749130e44ba645

See more details on using hashes here.

File details

Details for the file pydantic_socketio-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_socketio-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d05a066e9ed488e67f550944456b338e2317c637a038ffaa41b6f9323f8e07c3
MD5 e3354807578f8f6a545fc685796e164d
BLAKE2b-256 98990d40073ad324680cfe907667a2e386145307ccdbef6427683cfd878ebba0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page