{"id":26612312,"url":"https://github.com/atomiechen/pydantic-socketio","last_synced_at":"2025-04-10T01:35:11.230Z","repository":{"id":282688283,"uuid":"949266560","full_name":"atomiechen/Pydantic-SocketIO","owner":"atomiechen","description":"Pydantic-enhanced SocketIO with FastAPI integration support.","archived":false,"fork":false,"pushed_at":"2025-03-18T07:58:17.000Z","size":177,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T02:27:08.210Z","etag":null,"topics":["fastapi","pydantic","socket-io"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/pydantic-socketio/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/atomiechen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-16T03:43:44.000Z","updated_at":"2025-03-18T07:58:00.000Z","dependencies_parsed_at":"2025-03-16T10:27:27.167Z","dependency_job_id":"4754c9e4-f0fb-4699-bc1c-1060785d25a7","html_url":"https://github.com/atomiechen/Pydantic-SocketIO","commit_stats":null,"previous_names":["atomiechen/pydantic-socketio"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomiechen%2FPydantic-SocketIO","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomiechen%2FPydantic-SocketIO/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomiechen%2FPydantic-SocketIO/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomiechen%2FPydantic-SocketIO/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atomiechen","download_url":"https://codeload.github.com/atomiechen/Pydantic-SocketIO/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248141008,"owners_count":21054380,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","pydantic","socket-io"],"created_at":"2025-03-24T03:17:32.094Z","updated_at":"2025-04-10T01:35:11.208Z","avatar_url":"https://github.com/atomiechen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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\u0026logoColor=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⭐️ **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🪐 **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) © 2025 by [Atomie CHEN](https://github.com/atomiechen) is licensed under the [MIT License](https://github.com/atomiechen/Pydantic-SocketIO/blob/main/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomiechen%2Fpydantic-socketio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatomiechen%2Fpydantic-socketio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomiechen%2Fpydantic-socketio/lists"}