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

feature request : allow to provide a client_manager #10 #29

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bdba710
Merge pull request #1 from pyropy/add-socketio-fastapi-integration
pyropy Sep 15, 2020
3d3a1a0
Update example and README.md
pyropy Sep 15, 2020
b3ebfcc
Delete dummy tests 😅
pyropy Sep 15, 2020
eb0b6b9
Update test dependencies 🧐
pyropy Sep 15, 2020
d02fa00
Add dummy test
pyropy Sep 15, 2020
12a2232
Fix deploy workflow
pyropy Sep 15, 2020
bd66289
Up version to 0.0.3a
pyropy Nov 14, 2020
92e7368
Merge pull request #6 from pyropy/0.0.3a
pyropy Nov 14, 2020
608b329
Up version to 0.0.3
pyropy Nov 14, 2020
15c1884
Use cors_allowed_origins in AsyncServer
MatthewScholefield Nov 25, 2020
68ee072
Merge pull request #7 from MatthewScholefield/patch-1
pyropy Nov 25, 2020
ee83042
Add CONTRIBUTORS.md and update README.md
pyropy Nov 26, 2020
8726c7b
Merge pull request #8 from pyropy/add-contributors
pyropy Nov 26, 2020
e6ec802
Upgrade to release v0.0.4
pyropy Nov 26, 2020
e851a10
Add missing dependencies to setup.py
pyropy Dec 1, 2020
c0b197e
Update package dependnecies
pyropy Dec 1, 2020
7665715
Merge pull request #11 from pyropy/#9-add-missing-dependencies
pyropy Dec 1, 2020
05df44c
Update package version to 0.0.5
pyropy Dec 1, 2020
8e11b84
Update package version to 0.0.6
pyropy Dec 1, 2020
c67bd22
Remove unused dependencies
khiemdoan May 19, 2021
0635222
Merge pull request #18 from khiemdoan/dependencies
pyropy May 20, 2021
45cbddb
Add enter and leave room functions
pyropy Jun 17, 2021
8ab562e
Update contributors
pyropy Jun 17, 2021
e2016ce
Bump package version to 0.0.7
pyropy Jun 17, 2021
688846c
Merge pull request #20 from pyropy/add-join-room
pyropy Jun 17, 2021
079a951
Add async transport mode
pyropy Jun 17, 2021
9dc0b59
Bump package version
pyropy Jun 17, 2021
17d4d17
Merge pull request #21 from pyropy/add-async-transport-mode
pyropy Jun 17, 2021
9251fe9
Fixed #24 invalid enter_room and leave_room
Roxe322 Oct 28, 2021
367242c
Updated imports to PEP8
Roxe322 Oct 28, 2021
4e36d5f
Merge pull request #25 from Roxe322/master
pyropy Oct 30, 2021
5b02d60
Update setup.py
pyropy Dec 5, 2021
fc38dd1
add message queue suppoert for distributed application usage
kumaF Feb 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ jobs:
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
twine upload --skip-existing dist/*

6 changes: 6 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Contributors

* [Matthew D. Scholefield](https://github.com/MatthewScholefield) - Added cors allowed origins for AsyncServer [#7](https://github.com/pyropy/fastapi-socketio/pull/7)
* [Aaron Tolman](https://github.com/tolmanam) - Reported issue with broken pip download [#5](https://github.com/pyropy/fastapi-socketio/issues/5)
* [Khiem Doan](https://github.com/khiemdoan) - Helped with cleaning up old unused dependencies[#5](https://github.com/pyropy/fastapi-socketio/issues/15)
* [Artem Kolomatskiy](https://github.com/Roxe322) - Fixed invalid usage of enter_room and leave_room[#24](https://github.com/pyropy/fastapi-socketio/issues/24)
19 changes: 13 additions & 6 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ jedi = "*"
neovim = "*"

[packages]
fastapi = "==0.61.1"
netifaces = "==0.10.6"
pydantic = "==1.6.1"
starlette = "==0.13.6"
python-socketio = "==4.6.0"
fastapi = ">=0.61.1"
netifaces = ">=0.10.6"
pydantic = ">=1.6.1"
starlette = ">=0.13.6"
python-socketio = ">=4.6.0"
python-engineio = "*"

[test]
pytest = "==6.0.1"
pytest = ">=6.0.1"
fastapi = ">=0.61.1"
netifaces = ">=0.10.6"
pydantic = ">=1.6.1"
starlette = ">=0.13.6"
python-socketio = ">=4.6.0"
python-engineio = "*"


[requires]
python_version = "3.8"
141 changes: 75 additions & 66 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ async def handle_leave(sid, *args, **kwargs):

```

### Working with distributed applications

When working with distributed applications, it is often necessary to access the functionality of the Socket.IO from multiple processes. As a solution to the above problems, the Socket.IO server can be configured to connect to a message queue such as `Redis` or `RabbitMQ`, to communicate with other related Socket.IO servers or auxiliary workers.

Refer this link for more details https://python-socketio.readthedocs.io/en/latest/server.html#using-a-message-queue

```python
# app.py
import socketio

from fastapi import FastAPI
from fastapi_socketio import SocketManager

app = FastAPI()
redis_manager = socketio.AsyncRedisManager('redis://')

socket_manager = SocketManager(app=app, client_manager=redis_manager)
```

### Emitting from external process

```python
# emitter.py
import socketio

external_sio = socketio.RedisManager('redis://', write_only=True) # connect to the redis queue as an external process
external_sio.emit('my event', data={'foo': 'bar'}, room='my room') # emit an event
```
## Development

To contribute to this library, first checkout the code. Then create a new virtual environment:
Expand All @@ -68,3 +96,18 @@ Now install the dependencies and tests:
To run the tests:

pytest


## Run example

To run the examples simply run:

```bash
PYTHONPATH=. python examples/app.py
```

Before running example make sure you have all dependencies installed.

## Contributors

For list of contributors please reefer to CONTRIBUTORS.md file in this repository.
2 changes: 1 addition & 1 deletion examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ async def test(sid, *args, **kwargs):

import uvicorn

uvicorn.run("examples:app", host='0.0.0.0', port=8000, reload=True, debug=False)
uvicorn.run("examples.app:app", host='0.0.0.0', port=8000, reload=True, debug=False)
5 changes: 1 addition & 4 deletions fastapi_socketio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
from .socket_manager import SocketManager

def example_function():
return 1 + 1
from .socket_manager import SocketManager
23 changes: 21 additions & 2 deletions fastapi_socketio/socket_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import Union

import socketio

from socketio.asyncio_manager import AsyncManager
from fastapi import FastAPI


Expand All @@ -21,10 +25,13 @@ def __init__(
app: FastAPI,
mount_location: str = "/ws",
socketio_path: str = "socket.io",
cors_allowed_origins: list = [],
cors_allowed_origins: Union[str, list] = '*',
async_mode: str = "asgi",
client_manager=None
) -> None:
# TODO: Change Cors policy based on fastapi cors Middleware
self._sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
self._sio = socketio.AsyncServer(
async_mode=async_mode, cors_allowed_origins=cors_allowed_origins, client_manager=client_manager)
self._app = socketio.ASGIApp(
socketio_server=self._sio, socketio_path=socketio_path
)
Expand Down Expand Up @@ -86,3 +93,15 @@ def start_background_task(self):
@property
def sleep(self):
return self._sio.sleep

@property
def enter_room(self):
return self._sio.enter_room

@property
def leave_room(self):
return self._sio.leave_room

@property
def rooms(self):
return self._sio.rooms
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup
import os

VERSION = "0.1"
VERSION = "0.0.9"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a version downgrade?



def get_long_description():
Expand All @@ -28,7 +28,10 @@ def get_long_description():
license="Apache License, Version 2.0",
version=VERSION,
packages=["fastapi_socketio"],
install_requires=[],
install_requires=[
"fastapi>=0.61.1",
"python-socketio>=4.6.0",
],
extras_require={"test": ["pytest"]},
tests_require=["fastapi-socketio[test]"],
)
7 changes: 2 additions & 5 deletions tests/test_fastapi_socketio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
from fastapi_socketio import example_function


def test_example_function():
assert example_function() == 2
def test_dummy():
assert True