pydantic-socketio 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydantic_socketio-0.1.0/.gitattributes +4 -0
- pydantic_socketio-0.1.0/.github/workflows/publish.yml +76 -0
- pydantic_socketio-0.1.0/.gitignore +168 -0
- pydantic_socketio-0.1.0/CHANGELOG.md +16 -0
- pydantic_socketio-0.1.0/LICENSE +21 -0
- pydantic_socketio-0.1.0/PKG-INFO +165 -0
- pydantic_socketio-0.1.0/README.md +139 -0
- pydantic_socketio-0.1.0/pyproject.toml +50 -0
- pydantic_socketio-0.1.0/scripts/format.sh +5 -0
- pydantic_socketio-0.1.0/scripts/lint.sh +5 -0
- pydantic_socketio-0.1.0/src/pydantic_socketio/__init__.py +16 -0
- pydantic_socketio-0.1.0/src/pydantic_socketio/fastapi_socketio.py +40 -0
- pydantic_socketio-0.1.0/src/pydantic_socketio/py.typed +0 -0
- pydantic_socketio-0.1.0/src/pydantic_socketio/pydantic_socketio.py +174 -0
- pydantic_socketio-0.1.0/tests/test_client.py +25 -0
- pydantic_socketio-0.1.0/tests/test_fastapi.py +44 -0
- pydantic_socketio-0.1.0/tests/test_server.py +47 -0
- pydantic_socketio-0.1.0/uv.lock +2995 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
name: Publish Python 🐍 distributions 📦
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
inputs:
|
|
6
|
+
publish_testpypi:
|
|
7
|
+
type: boolean
|
|
8
|
+
default: false
|
|
9
|
+
description: Publish to TestPyPI
|
|
10
|
+
publish_pypi:
|
|
11
|
+
type: boolean
|
|
12
|
+
default: false
|
|
13
|
+
description: Publish to PyPI
|
|
14
|
+
publish_gh_release:
|
|
15
|
+
type: boolean
|
|
16
|
+
default: true
|
|
17
|
+
description: Publish to GitHub Release
|
|
18
|
+
use_changelog:
|
|
19
|
+
type: boolean
|
|
20
|
+
default: true
|
|
21
|
+
description: Extract release notes from CHANGELOG.md
|
|
22
|
+
changelog_file:
|
|
23
|
+
type: string
|
|
24
|
+
default: CHANGELOG.md
|
|
25
|
+
description: Path to changelog file
|
|
26
|
+
required: false
|
|
27
|
+
release_tag:
|
|
28
|
+
type: string
|
|
29
|
+
description: Tag to package (empty for latest tag)
|
|
30
|
+
required: false
|
|
31
|
+
|
|
32
|
+
jobs:
|
|
33
|
+
get-tag:
|
|
34
|
+
runs-on: ubuntu-latest
|
|
35
|
+
outputs:
|
|
36
|
+
release_tag: ${{ steps.set_release_tag.outputs.tag }}
|
|
37
|
+
steps:
|
|
38
|
+
- name: Checkout code
|
|
39
|
+
uses: actions/checkout@v4
|
|
40
|
+
- name: Fetch all tags
|
|
41
|
+
run: |
|
|
42
|
+
git fetch --prune --unshallow --tags
|
|
43
|
+
- name: Verify and set release tag
|
|
44
|
+
id: set_release_tag
|
|
45
|
+
run: |
|
|
46
|
+
release_tag=${{ inputs.release_tag }}
|
|
47
|
+
if [ -z "$release_tag" ]; then
|
|
48
|
+
echo "Input tag is empty. Fetching latest tag."
|
|
49
|
+
release_tag=$(git describe --tags $(git rev-list --tags --max-count=1))
|
|
50
|
+
if [ -z "$release_tag" ]; then
|
|
51
|
+
echo "No latest tag available. Exiting workflow."
|
|
52
|
+
exit 1
|
|
53
|
+
fi
|
|
54
|
+
else
|
|
55
|
+
if ! git rev-parse -q --verify "refs/tags/$release_tag" >/dev/null; then
|
|
56
|
+
echo "Invalid tag '$release_tag'. Exiting workflow."
|
|
57
|
+
exit 1
|
|
58
|
+
fi
|
|
59
|
+
fi
|
|
60
|
+
echo "tag=$release_tag" >> $GITHUB_OUTPUT
|
|
61
|
+
build-n-publish:
|
|
62
|
+
needs: get-tag
|
|
63
|
+
permissions:
|
|
64
|
+
id-token: write # IMPORTANT: mandatory for trusted publishing and sigstore
|
|
65
|
+
contents: write # IMPORTANT: mandatory for making GitHub Releases
|
|
66
|
+
uses: atomiechen/reusable-workflows/.github/workflows/publish-python-distributions.yml@main
|
|
67
|
+
with:
|
|
68
|
+
publish_testpypi: ${{ inputs.publish_testpypi }}
|
|
69
|
+
publish_pypi: ${{ inputs.publish_pypi }}
|
|
70
|
+
publish_gh_release: ${{ inputs.publish_gh_release }}
|
|
71
|
+
use_changelog: ${{ inputs.use_changelog }}
|
|
72
|
+
changelog_file: ${{ inputs.changelog_file }}
|
|
73
|
+
release_tag: ${{ needs.get-tag.outputs.release_tag }}
|
|
74
|
+
secrets:
|
|
75
|
+
TEST_PYPI_API_TOKEN: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
|
76
|
+
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# ruff
|
|
2
|
+
.ruff_cache
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# == Below are commonly ignored files for Python projects == #
|
|
6
|
+
|
|
7
|
+
# Byte-compiled / optimized / DLL files
|
|
8
|
+
__pycache__/
|
|
9
|
+
*.py[cod]
|
|
10
|
+
*$py.class
|
|
11
|
+
|
|
12
|
+
# C extensions
|
|
13
|
+
*.so
|
|
14
|
+
|
|
15
|
+
# Distribution / packaging
|
|
16
|
+
.Python
|
|
17
|
+
build/
|
|
18
|
+
develop-eggs/
|
|
19
|
+
dist/
|
|
20
|
+
downloads/
|
|
21
|
+
eggs/
|
|
22
|
+
.eggs/
|
|
23
|
+
lib/
|
|
24
|
+
lib64/
|
|
25
|
+
parts/
|
|
26
|
+
sdist/
|
|
27
|
+
var/
|
|
28
|
+
wheels/
|
|
29
|
+
share/python-wheels/
|
|
30
|
+
*.egg-info/
|
|
31
|
+
.installed.cfg
|
|
32
|
+
*.egg
|
|
33
|
+
MANIFEST
|
|
34
|
+
|
|
35
|
+
# PyInstaller
|
|
36
|
+
# Usually these files are written by a python script from a template
|
|
37
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
38
|
+
*.manifest
|
|
39
|
+
*.spec
|
|
40
|
+
|
|
41
|
+
# Installer logs
|
|
42
|
+
pip-log.txt
|
|
43
|
+
pip-delete-this-directory.txt
|
|
44
|
+
|
|
45
|
+
# Unit test / coverage reports
|
|
46
|
+
htmlcov/
|
|
47
|
+
.tox/
|
|
48
|
+
.nox/
|
|
49
|
+
.coverage
|
|
50
|
+
.coverage.*
|
|
51
|
+
.cache
|
|
52
|
+
nosetests.xml
|
|
53
|
+
coverage.xml
|
|
54
|
+
*.cover
|
|
55
|
+
*.py,cover
|
|
56
|
+
.hypothesis/
|
|
57
|
+
.pytest_cache/
|
|
58
|
+
cover/
|
|
59
|
+
|
|
60
|
+
# Translations
|
|
61
|
+
*.mo
|
|
62
|
+
*.pot
|
|
63
|
+
|
|
64
|
+
# Django stuff:
|
|
65
|
+
*.log
|
|
66
|
+
local_settings.py
|
|
67
|
+
db.sqlite3
|
|
68
|
+
db.sqlite3-journal
|
|
69
|
+
|
|
70
|
+
# Flask stuff:
|
|
71
|
+
instance/
|
|
72
|
+
.webassets-cache
|
|
73
|
+
|
|
74
|
+
# Scrapy stuff:
|
|
75
|
+
.scrapy
|
|
76
|
+
|
|
77
|
+
# Sphinx documentation
|
|
78
|
+
docs/_build/
|
|
79
|
+
|
|
80
|
+
# PyBuilder
|
|
81
|
+
.pybuilder/
|
|
82
|
+
target/
|
|
83
|
+
|
|
84
|
+
# Jupyter Notebook
|
|
85
|
+
.ipynb_checkpoints
|
|
86
|
+
|
|
87
|
+
# IPython
|
|
88
|
+
profile_default/
|
|
89
|
+
ipython_config.py
|
|
90
|
+
|
|
91
|
+
# pyenv
|
|
92
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
93
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
94
|
+
# .python-version
|
|
95
|
+
|
|
96
|
+
# pipenv
|
|
97
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
98
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
99
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
100
|
+
# install all needed dependencies.
|
|
101
|
+
#Pipfile.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to Pydantic-SocketIO will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2025-03-16
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
Initial features:
|
|
13
|
+
|
|
14
|
+
- Pydantic enhanced socketio server and client (both sync and async). They should be drop-in replacements for the original socketio server and client.
|
|
15
|
+
- Alternatively, monkey patching method `monkey_patch()` for the original socketio server and client.
|
|
16
|
+
- Integration with fastapi `FastAPISocketIO`.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Atomie CHEN
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pydantic-socketio
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pydantic-enhanced SocketIO with FastAPI integration support.
|
|
5
|
+
Project-URL: homepage, https://github.com/atomiechen/Pydantic-SocketIO
|
|
6
|
+
Project-URL: issues, https://github.com/atomiechen/Pydantic-SocketIO/issues
|
|
7
|
+
Project-URL: changelog, https://github.com/atomiechen/Pydantic-SocketIO/blob/main/CHANGELOG.md
|
|
8
|
+
Author-email: Atomie CHEN <atomic_cwh@163.com>
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: fastapi,pydantic,socketio
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Requires-Dist: pydantic>=2.10.6
|
|
16
|
+
Requires-Dist: python-socketio>=5.12.1
|
|
17
|
+
Provides-Extra: asyncio-client
|
|
18
|
+
Requires-Dist: python-socketio[asyncio-client]>=5.12.1; extra == 'asyncio-client'
|
|
19
|
+
Provides-Extra: client
|
|
20
|
+
Requires-Dist: python-socketio[client]>=5.12.1; extra == 'client'
|
|
21
|
+
Provides-Extra: docs
|
|
22
|
+
Requires-Dist: python-socketio[docs]>=5.12.1; extra == 'docs'
|
|
23
|
+
Provides-Extra: fastapi
|
|
24
|
+
Requires-Dist: fastapi[standard]>=0.115.11; extra == 'fastapi'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Pydantic-SocketIO
|
|
28
|
+
|
|
29
|
+
A Pydantic-enhanced SocketIO library for Python, with FastAPI integration.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
⭐️ **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.
|
|
35
|
+
|
|
36
|
+
🪐 **Easy Integration with FastAPI**: Seamlessly integrates `Socket.IO` with FastAPI, allowing you to manage event-driven communication effortlessly.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
pip install pydantic-socketio
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### Recommended: Pydantic-Enhanced SocketIO Server and Client
|
|
49
|
+
|
|
50
|
+
Drop-in replacements for the original [python-socketio](https://github.com/miguelgrinberg/python-socketio) server and client are provided.
|
|
51
|
+
|
|
52
|
+
The enhanced SocketIO server with Pydantic validation:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from pydantic import BaseModel
|
|
56
|
+
import pydantic_socketio
|
|
57
|
+
|
|
58
|
+
class ChatMessage(BaseModel):
|
|
59
|
+
role: str
|
|
60
|
+
content: str
|
|
61
|
+
|
|
62
|
+
# Create an enhanced SocketIO server; use AsyncServer for async server
|
|
63
|
+
sio = pydantic_socketio.Server()
|
|
64
|
+
|
|
65
|
+
# Define an event with Pydantic validation
|
|
66
|
+
@sio.event
|
|
67
|
+
def message(data: ChatMessage):
|
|
68
|
+
print(f"Received chat message from {data.role}: {data.content}")
|
|
69
|
+
data.content = data.content.upper()
|
|
70
|
+
print(f"Sending uppercase message: {data.content}")
|
|
71
|
+
# Emit an event with Pydantic model without any additional conversion
|
|
72
|
+
sio.emit("message", data)
|
|
73
|
+
|
|
74
|
+
# `on` decorator is also supported
|
|
75
|
+
@sio.on("custom_event")
|
|
76
|
+
def handle_custom_event(data: int):
|
|
77
|
+
...
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The enhanced SocketIO client with Pydantic validation:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import pydantic_socketio
|
|
84
|
+
|
|
85
|
+
# Create an enhanced SocketIO client; use AsyncClient for async client
|
|
86
|
+
sio = pydantic_socketio.Client()
|
|
87
|
+
|
|
88
|
+
@sio.event
|
|
89
|
+
def ping(data: int):
|
|
90
|
+
...
|
|
91
|
+
|
|
92
|
+
@sio.on("pong")
|
|
93
|
+
def handle_pong(data: int):
|
|
94
|
+
...
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
### Alternative: Monkey Patching for Original SocketIO
|
|
99
|
+
|
|
100
|
+
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:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from pydantic_socketio import monkey_patch
|
|
104
|
+
import socketio
|
|
105
|
+
|
|
106
|
+
# Apply monkey patch to the original socketio server and client
|
|
107
|
+
monkey_patch()
|
|
108
|
+
|
|
109
|
+
# Now, you can use the original socketio server and client with Pydantic validation
|
|
110
|
+
sio = socketio.Server()
|
|
111
|
+
|
|
112
|
+
@sio.event
|
|
113
|
+
def ping(data: int):
|
|
114
|
+
print(f"Received ping: {data}")
|
|
115
|
+
data += 1
|
|
116
|
+
print(f"Sending pong: {data}")
|
|
117
|
+
sio.emit("poing", data)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
### FastAPI Integration
|
|
122
|
+
|
|
123
|
+
You can easily integrate the enhanced socketio server with FastAPI by using FastAPISocketIO:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from fastapi import FastAPI
|
|
127
|
+
from pydantic_socketio import FastAPISocketIO
|
|
128
|
+
|
|
129
|
+
app = FastAPI()
|
|
130
|
+
|
|
131
|
+
@app.get("/")
|
|
132
|
+
async def root():
|
|
133
|
+
return {"message": "Hello World"}
|
|
134
|
+
...
|
|
135
|
+
|
|
136
|
+
# Create a FastAPI socketio server
|
|
137
|
+
sio = FastAPISocketIO(app)
|
|
138
|
+
|
|
139
|
+
@sio.event
|
|
140
|
+
async def ping(data: int):
|
|
141
|
+
print(f"Received ping: {data}")
|
|
142
|
+
data += 1
|
|
143
|
+
print(f"Sending pong: {data}")
|
|
144
|
+
await sio.emit("pong", data)
|
|
145
|
+
...
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
You can also integrate the SocketIO server manually after FastAPI initialization:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from fastapi import FastAPI
|
|
152
|
+
from pydantic_socketio import FastAPISocketIO
|
|
153
|
+
|
|
154
|
+
sio = FastAPISocketIO()
|
|
155
|
+
...
|
|
156
|
+
app = FastAPI()
|
|
157
|
+
...
|
|
158
|
+
|
|
159
|
+
# Integrate the SocketIO server to FastAPI
|
|
160
|
+
sio.integrate(app)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
[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).
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Pydantic-SocketIO
|
|
2
|
+
|
|
3
|
+
A Pydantic-enhanced SocketIO library for Python, with FastAPI integration.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
⭐️ **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.
|
|
9
|
+
|
|
10
|
+
🪐 **Easy Integration with FastAPI**: Seamlessly integrates `Socket.IO` with FastAPI, allowing you to manage event-driven communication effortlessly.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pip install pydantic-socketio
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Recommended: Pydantic-Enhanced SocketIO Server and Client
|
|
23
|
+
|
|
24
|
+
Drop-in replacements for the original [python-socketio](https://github.com/miguelgrinberg/python-socketio) server and client are provided.
|
|
25
|
+
|
|
26
|
+
The enhanced SocketIO server with Pydantic validation:
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from pydantic import BaseModel
|
|
30
|
+
import pydantic_socketio
|
|
31
|
+
|
|
32
|
+
class ChatMessage(BaseModel):
|
|
33
|
+
role: str
|
|
34
|
+
content: str
|
|
35
|
+
|
|
36
|
+
# Create an enhanced SocketIO server; use AsyncServer for async server
|
|
37
|
+
sio = pydantic_socketio.Server()
|
|
38
|
+
|
|
39
|
+
# Define an event with Pydantic validation
|
|
40
|
+
@sio.event
|
|
41
|
+
def message(data: ChatMessage):
|
|
42
|
+
print(f"Received chat message from {data.role}: {data.content}")
|
|
43
|
+
data.content = data.content.upper()
|
|
44
|
+
print(f"Sending uppercase message: {data.content}")
|
|
45
|
+
# Emit an event with Pydantic model without any additional conversion
|
|
46
|
+
sio.emit("message", data)
|
|
47
|
+
|
|
48
|
+
# `on` decorator is also supported
|
|
49
|
+
@sio.on("custom_event")
|
|
50
|
+
def handle_custom_event(data: int):
|
|
51
|
+
...
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The enhanced SocketIO client with Pydantic validation:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import pydantic_socketio
|
|
58
|
+
|
|
59
|
+
# Create an enhanced SocketIO client; use AsyncClient for async client
|
|
60
|
+
sio = pydantic_socketio.Client()
|
|
61
|
+
|
|
62
|
+
@sio.event
|
|
63
|
+
def ping(data: int):
|
|
64
|
+
...
|
|
65
|
+
|
|
66
|
+
@sio.on("pong")
|
|
67
|
+
def handle_pong(data: int):
|
|
68
|
+
...
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
### Alternative: Monkey Patching for Original SocketIO
|
|
73
|
+
|
|
74
|
+
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:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from pydantic_socketio import monkey_patch
|
|
78
|
+
import socketio
|
|
79
|
+
|
|
80
|
+
# Apply monkey patch to the original socketio server and client
|
|
81
|
+
monkey_patch()
|
|
82
|
+
|
|
83
|
+
# Now, you can use the original socketio server and client with Pydantic validation
|
|
84
|
+
sio = socketio.Server()
|
|
85
|
+
|
|
86
|
+
@sio.event
|
|
87
|
+
def ping(data: int):
|
|
88
|
+
print(f"Received ping: {data}")
|
|
89
|
+
data += 1
|
|
90
|
+
print(f"Sending pong: {data}")
|
|
91
|
+
sio.emit("poing", data)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
### FastAPI Integration
|
|
96
|
+
|
|
97
|
+
You can easily integrate the enhanced socketio server with FastAPI by using FastAPISocketIO:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from fastapi import FastAPI
|
|
101
|
+
from pydantic_socketio import FastAPISocketIO
|
|
102
|
+
|
|
103
|
+
app = FastAPI()
|
|
104
|
+
|
|
105
|
+
@app.get("/")
|
|
106
|
+
async def root():
|
|
107
|
+
return {"message": "Hello World"}
|
|
108
|
+
...
|
|
109
|
+
|
|
110
|
+
# Create a FastAPI socketio server
|
|
111
|
+
sio = FastAPISocketIO(app)
|
|
112
|
+
|
|
113
|
+
@sio.event
|
|
114
|
+
async def ping(data: int):
|
|
115
|
+
print(f"Received ping: {data}")
|
|
116
|
+
data += 1
|
|
117
|
+
print(f"Sending pong: {data}")
|
|
118
|
+
await sio.emit("pong", data)
|
|
119
|
+
...
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
You can also integrate the SocketIO server manually after FastAPI initialization:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from fastapi import FastAPI
|
|
126
|
+
from pydantic_socketio import FastAPISocketIO
|
|
127
|
+
|
|
128
|
+
sio = FastAPISocketIO()
|
|
129
|
+
...
|
|
130
|
+
app = FastAPI()
|
|
131
|
+
...
|
|
132
|
+
|
|
133
|
+
# Integrate the SocketIO server to FastAPI
|
|
134
|
+
sio.integrate(app)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
[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).
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pydantic-socketio"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Pydantic-enhanced SocketIO with FastAPI integration support."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Atomie CHEN", email = "atomic_cwh@163.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.8"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
]
|
|
15
|
+
keywords = ["pydantic", "socketio", "fastapi"]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"pydantic>=2.10.6",
|
|
18
|
+
"python-socketio>=5.12.1",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
homepage = "https://github.com/atomiechen/Pydantic-SocketIO"
|
|
23
|
+
issues = "https://github.com/atomiechen/Pydantic-SocketIO/issues"
|
|
24
|
+
changelog = "https://github.com/atomiechen/Pydantic-SocketIO/blob/main/CHANGELOG.md"
|
|
25
|
+
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
fastapi = [
|
|
28
|
+
"fastapi[standard]>=0.115.11",
|
|
29
|
+
]
|
|
30
|
+
client = [
|
|
31
|
+
"python-socketio[client]>=5.12.1",
|
|
32
|
+
]
|
|
33
|
+
asyncio-client = [
|
|
34
|
+
"python-socketio[asyncio-client]>=5.12.1",
|
|
35
|
+
]
|
|
36
|
+
docs = [
|
|
37
|
+
"python-socketio[docs]>=5.12.1",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[build-system]
|
|
41
|
+
requires = ["hatchling"]
|
|
42
|
+
build-backend = "hatchling.build"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = [
|
|
46
|
+
"eventlet>=0.39.1",
|
|
47
|
+
"fastapi[standard]>=0.115.11",
|
|
48
|
+
"python-socketio[client]>=5.12.1",
|
|
49
|
+
"ruff>=0.11.0",
|
|
50
|
+
]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# first import everything from socketio
|
|
2
|
+
from socketio import * # type: ignore # noqa: F403
|
|
3
|
+
|
|
4
|
+
from .pydantic_socketio import (
|
|
5
|
+
BaseClient as BaseClient,
|
|
6
|
+
Client as Client,
|
|
7
|
+
AsyncClient as AsyncClient,
|
|
8
|
+
BaseServer as BaseServer,
|
|
9
|
+
Server as Server,
|
|
10
|
+
AsyncServer as AsyncServer,
|
|
11
|
+
monkey_patch as monkey_patch,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
from .fastapi_socketio import (
|
|
15
|
+
FastAPISocketIO as FastAPISocketIO,
|
|
16
|
+
)
|