Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit 786c2f1

Browse files
committed
Add basic SocketIO wrapper for FastAPI
1 parent 011c681 commit 786c2f1

File tree

7 files changed

+208
-73
lines changed

7 files changed

+208
-73
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ venv
77
.pytest_cache
88
*.egg-info
99
.DS_Store
10+
.vscode/

Pipfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ neovim = "*"
1111
fastapi = "==0.61.1"
1212
netifaces = "==0.10.6"
1313
pydantic = "==1.6.1"
14-
socketio = "==0.2.1"
1514
starlette = "==0.13.6"
15+
python-socketio = "==4.6.0"
16+
python-engineio = "*"
1617

1718
[test]
1819
pytest = "==6.0.1"

Pipfile.lock

+85-71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,40 @@ Install this plugin using `pip`:
1414

1515
## Usage
1616

17-
Usage instructions go here.
17+
To add SocketIO support to FastAPI all you need to do is import `SocketManager` and pass it `FastAPI` object.
18+
19+
```python
20+
# app.py
21+
from fastapi import FastAPI
22+
from fastapi_socketio import SocketManager
23+
24+
app = FastAPI()
25+
socket_manager = SocketManager(app=app)
26+
```
27+
28+
29+
Now you can use SocketIO directly from your `FastAPI` app object.
30+
```python
31+
# socket_handlers.py
32+
from .app import app
33+
34+
@app.sio.on('join')
35+
async def handle_join(sid, *args, **kwargs):
36+
await app.sio.emit('lobby', 'User joined')
37+
38+
```
39+
40+
Or you can import `SocketManager` object that exposes most of the SocketIO functionality.
41+
42+
```python
43+
# socket_handlers2.py
44+
from .app import socket_manager as sm
45+
46+
@sm.on('leave')
47+
async def handle_leave(sid, *args, **kwargs):
48+
await sm.emit('lobby', 'User left')
49+
50+
```
1851

1952
## Development
2053

fastapi_socketio/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
from .socket_manager import SocketManager
2+
13
def example_function():
24
return 1 + 1

fastapi_socketio/socket_manager.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import socketio
2+
from fastapi import FastAPI
3+
4+
5+
class SocketManager:
6+
"""
7+
Creates and mounts SocketIO app to FastAPI app.
8+
Default mount location for SocketIO app is "/ws".
9+
10+
Exponses basic functionality of SocketIO.
11+
12+
Default socketio_path is "socket.io"
13+
"""
14+
15+
def __init__(
16+
self,
17+
app: FastAPI,
18+
mount_location: str = "/ws",
19+
socketio_path: str = "socket.io",
20+
cors_allowed_origins: list = [],
21+
) -> None:
22+
23+
self._sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
24+
self._app = socketio.ASGIApp(
25+
socketio_server=self._sio, socketio_path=socketio_path
26+
)
27+
28+
app.mount(mount_location, self._app)
29+
app.sio = self._sio
30+
31+
def is_asyncio_based(self) -> bool:
32+
return True
33+
34+
@property
35+
def on(self):
36+
return self._sio.on
37+
38+
@property
39+
def attach(self):
40+
return self._sio.attach
41+
42+
@property
43+
def emit(self):
44+
return self._sio.emit
45+
46+
@property
47+
def send(self):
48+
return self._sio.send
49+
50+
@property
51+
def call(self):
52+
return self._sio.call
53+
54+
@property
55+
def close_room(self):
56+
return self._sio.close_room
57+
58+
@property
59+
def get_session(self):
60+
return self._sio.get_session
61+
62+
@property
63+
def save_session(self):
64+
return self._sio.save_session
65+
66+
@property
67+
def session(self):
68+
return self._sio.session
69+
70+
@property
71+
def disconnect(self):
72+
return self._sio.disconnect
73+
74+
@property
75+
def handle_request(self):
76+
return self._sio.handle_request
77+
78+
@property
79+
def start_background_task(self):
80+
return self._sio.start_background_task
81+
82+
@property
83+
def sleep(self):
84+
return self._sio.sleep

fastapi_socketio/socket_router.py

Whitespace-only changes.

0 commit comments

Comments
 (0)