Easly integrate socket.io with your FastAPI app.
Project description
fastapi-socketio
Easly integrate socket.io with your FastAPI app.
Installation
Install this plugin using pip
:
$ pip install fastapi-socketio
Usage
To add SocketIO support to FastAPI all you need to do is import SocketManager
and pass it FastAPI
object.
# app.py
from fastapi import FastAPI
from fastapi_socketio import SocketManager
app = FastAPI()
socket_manager = SocketManager(app=app)
Now you can use SocketIO directly from your FastAPI
app object.
# socket_handlers.py
from .app import app
@app.sio.on('join')
async def handle_join(sid, *args, **kwargs):
await app.sio.emit('lobby', 'User joined')
Or you can import SocketManager
object that exposes most of the SocketIO functionality.
# socket_handlers2.py
from .app import socket_manager as sm
@sm.on('leave')
async def handle_leave(sid, *args, **kwargs):
await sm.emit('lobby', 'User left')
Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd fastapi-socketio
python -mvenv venv
source venv/bin/activate
Or if you are using pipenv
:
pipenv shell
Now install the dependencies and tests:
pip install -e '.[test]'
To run the tests:
pytest
Run example
To run the examples simply run:
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fastapi_socketio-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36666f99566ab81d1e600d7a95f86e575523f758c914a2cfd9ce9d8f9c8a0073 |
|
MD5 | 711341fdc1589822c20c1af65b21fc47 |
|
BLAKE2b-256 | 063f82b518d88553777d35a0bcc09f50583eec19cd24223e4941230a990a1d3b |