pydantic-socketio


Namepydantic-socketio JSON
Version 0.1.3 PyPI version JSON
download
home_pageNone
SummaryPydantic-enhanced SocketIO with FastAPI integration support.
upload_time2025-10-08 08:31:32
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords fastapi pydantic socketio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pydantic-SocketIO

[![GitHub](https://img.shields.io/badge/github-Pydantic--SocketIO-blue?logo=github)](https://github.com/atomiechen/Pydantic-SocketIO)
[![PyPI](https://img.shields.io/pypi/v/Pydantic--SocketIO?logo=pypi&logoColor=white)](https://pypi.org/project/pydantic-socketio/)


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


## Features

⭐️ **Pydantic-Enhanced SocketIO**: Drop-in replacements for the original [python-socketio](https://github.com/miguelgrinberg/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

```sh
pip install pydantic-socketio
```

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

```sh
pip install pydantic-socketio[fastapi]
```

Other options of original [python-socketio](https://github.com/miguelgrinberg/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](https://github.com/miguelgrinberg/python-socketio) server and client are provided. 

The enhanced SocketIO server with Pydantic validation:

```python
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:

```python
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](https://github.com/miguelgrinberg/python-socketio) server and client without replacing them, you can use the `monkey_patch()` method:

```python
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:

```python
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:

```python
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:

```python
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](https://python-socketio.readthedocs.io/en/stable/).


## License

[Pydantic-SocketIO](https://github.com/atomiechen/Pydantic-SocketIO) © 2025 by [Atomie CHEN](https://github.com/atomiechen) is licensed under the [MIT License](https://github.com/atomiechen/Pydantic-SocketIO/blob/main/LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pydantic-socketio",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "fastapi, pydantic, socketio",
    "author": null,
    "author_email": "Atomie CHEN <atomic_cwh@163.com>",
    "download_url": "https://files.pythonhosted.org/packages/91/d7/79fcfd852c1bd9a7ef055faff5a0f5e2dcb7097f1aac33749130e44ba645/pydantic_socketio-0.1.3.tar.gz",
    "platform": null,
    "description": "# Pydantic-SocketIO\n\n[![GitHub](https://img.shields.io/badge/github-Pydantic--SocketIO-blue?logo=github)](https://github.com/atomiechen/Pydantic-SocketIO)\n[![PyPI](https://img.shields.io/pypi/v/Pydantic--SocketIO?logo=pypi&logoColor=white)](https://pypi.org/project/pydantic-socketio/)\n\n\nA Pydantic-enhanced SocketIO library for Python, with FastAPI integration.\n\n\n## Features\n\n\u2b50\ufe0f **Pydantic-Enhanced SocketIO**: Drop-in replacements for the original [python-socketio](https://github.com/miguelgrinberg/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.\n\n\ud83e\ude90 **Easy Integration with FastAPI**: Seamlessly integrates `Socket.IO` with FastAPI, allowing you to manage event-driven communication effortlessly.\n\n\n## Installation\n\n```sh\npip install pydantic-socketio\n```\n\nIf you want FastAPI integration, you can install the extra dependencies:\n\n```sh\npip install pydantic-socketio[fastapi]\n```\n\nOther options of original [python-socketio](https://github.com/miguelgrinberg/python-socketio) are also available: `client`, `asyncio-client`, `docs`.\n\n\n## Usage\n\n### Recommended: Pydantic-Enhanced SocketIO Server and Client\n\nDrop-in replacements for the original [python-socketio](https://github.com/miguelgrinberg/python-socketio) server and client are provided. \n\nThe enhanced SocketIO server with Pydantic validation:\n\n```python\nfrom pydantic import BaseModel\nimport pydantic_socketio\n\nclass ChatMessage(BaseModel):\n    role: str\n    content: str\n\n# Create an enhanced SocketIO server; use AsyncServer for async server\nsio = pydantic_socketio.Server()\n\n# Define an event with Pydantic validation\n@sio.event\ndef message(data: ChatMessage):\n    print(f\"Received chat message from {data.role}: {data.content}\")\n    data.content = data.content.upper()\n    print(f\"Sending uppercase message: {data.content}\")\n    # Emit an event with Pydantic model without any additional conversion\n    sio.emit(\"message\", data)\n\n# `on` decorator is also supported\n@sio.on(\"custom_event\")\ndef handle_custom_event(data: int):\n    ...\n```\n\nThe enhanced SocketIO client with Pydantic validation:\n\n```python\nimport pydantic_socketio\n\n# Create an enhanced SocketIO client; use AsyncClient for async client\nsio = pydantic_socketio.Client()\n\n@sio.event\ndef ping(data: int):\n    ...\n\n@sio.on(\"pong\")\ndef handle_pong(data: int):\n    ...\n```\n\n\n### Alternative: Monkey Patching for Original SocketIO\n\nAlternatively, if you want to apply Pydantic validation to the original [python-socketio](https://github.com/miguelgrinberg/python-socketio) server and client without replacing them, you can use the `monkey_patch()` method:\n\n```python\nfrom pydantic_socketio import monkey_patch\nimport socketio\n\n# Apply monkey patch to the original socketio server and client\nmonkey_patch()\n\n# Now, you can use the original socketio server and client with Pydantic validation\nsio = socketio.Server()\n\n@sio.event\ndef ping(data: int):\n    print(f\"Received ping: {data}\")\n    data += 1\n    print(f\"Sending pong: {data}\")\n    sio.emit(\"poing\", data)\n```\n\n\n### FastAPI Integration\n\nYou can easily integrate the enhanced socketio server with FastAPI by using FastAPISocketIO:\n\n```python\nfrom fastapi import FastAPI\nfrom pydantic_socketio import FastAPISocketIO\n\napp = FastAPI()\n\n@app.get(\"/\")\nasync def root():\n    return {\"message\": \"Hello World\"}\n...\n\n# Create a FastAPI socketio server\nsio = FastAPISocketIO(app)\n\n@sio.event\nasync def ping(data: int):\n    print(f\"Received ping: {data}\")\n    data += 1\n    print(f\"Sending pong: {data}\")\n    await sio.emit(\"pong\", data)\n\n# Both sync and async event handlers are supported, as per the original python-socketio\n@sio.on(\"custom_event\")\ndef handle_custom_event(data: int):\n    ...\n```\n\nYou can also integrate the SocketIO server manually after FastAPI initialization:\n\n```python\nfrom fastapi import FastAPI\nfrom pydantic_socketio import FastAPISocketIO\n\nsio = FastAPISocketIO()\n...\napp = FastAPI()\n...\n\n# Integrate the SocketIO server to FastAPI\nsio.integrate(app)\n```\n\n\n### FastAPI Dependency Injection\n\nYou can use `SioDep` as a `FastAPISocketIO` dependency injection in FastAPI applications:\n\n```python\nfrom fastapi import FastAPI\nfrom pydantic_socketio import FastAPISocketIO, SioDep\n\napp = FastAPI()\nsio = FastAPISocketIO(app)\n\n# You may define this endpoint in another file, like in a separate router\n@app.get(\"/\")\nasync def root(sio: SioDep):\n    await sio.emit(\"message\", \"API root called\")\n    return {\"Hello\": \"World\"}\n```\n\n\n## Original Documentation\n\nMore details can be found in the original [python-socketio documentation](https://python-socketio.readthedocs.io/en/stable/).\n\n\n## License\n\n[Pydantic-SocketIO](https://github.com/atomiechen/Pydantic-SocketIO) \u00a9 2025 by [Atomie CHEN](https://github.com/atomiechen) is licensed under the [MIT License](https://github.com/atomiechen/Pydantic-SocketIO/blob/main/LICENSE).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Pydantic-enhanced SocketIO with FastAPI integration support.",
    "version": "0.1.3",
    "project_urls": {
        "changelog": "https://github.com/atomiechen/Pydantic-SocketIO/blob/main/CHANGELOG.md",
        "homepage": "https://github.com/atomiechen/Pydantic-SocketIO",
        "issues": "https://github.com/atomiechen/Pydantic-SocketIO/issues"
    },
    "split_keywords": [
        "fastapi",
        " pydantic",
        " socketio"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "98990d40073ad324680cfe907667a2e386145307ccdbef6427683cfd878ebba0",
                "md5": "e3354807578f8f6a545fc685796e164d",
                "sha256": "d05a066e9ed488e67f550944456b338e2317c637a038ffaa41b6f9323f8e07c3"
            },
            "downloads": -1,
            "filename": "pydantic_socketio-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e3354807578f8f6a545fc685796e164d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7774,
            "upload_time": "2025-10-08T08:31:31",
            "upload_time_iso_8601": "2025-10-08T08:31:31.228398Z",
            "url": "https://files.pythonhosted.org/packages/98/99/0d40073ad324680cfe907667a2e386145307ccdbef6427683cfd878ebba0/pydantic_socketio-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "91d779fcfd852c1bd9a7ef055faff5a0f5e2dcb7097f1aac33749130e44ba645",
                "md5": "c85606a5d01ce97ec043b119f1b67e92",
                "sha256": "b030a54eb2949db9c330ff5c6619886b5f13825c766de3ce5fcdb6f0709e37e4"
            },
            "downloads": -1,
            "filename": "pydantic_socketio-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "c85606a5d01ce97ec043b119f1b67e92",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 200538,
            "upload_time": "2025-10-08T08:31:32",
            "upload_time_iso_8601": "2025-10-08T08:31:32.291173Z",
            "url": "https://files.pythonhosted.org/packages/91/d7/79fcfd852c1bd9a7ef055faff5a0f5e2dcb7097f1aac33749130e44ba645/pydantic_socketio-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-08 08:31:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "atomiechen",
    "github_project": "Pydantic-SocketIO",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pydantic-socketio"
}
        
Elapsed time: 2.72660s