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

Repository files navigation

fastapi-socketio

PyPIChangelogLicense

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.pyfromfastapiimportFastAPIfromfastapi_socketioimportSocketManagerapp=FastAPI()
socket_manager=SocketManager(app=app)

Now you can use SocketIO directly from your FastAPI app object.

# socket_handlers.pyfrom .appimportapp@app.sio.on('join')asyncdefhandle_join(sid, *args, **kwargs):
awaitapp.sio.emit('lobby', 'User joined')

Or you can import SocketManager object that exposes most of the SocketIO functionality.

# socket_handlers2.pyfrom .appimportsocket_managerassm@sm.on('leave')asyncdefhandle_leave(sid, *args, **kwargs):
awaitsm.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.

About

Easily integrate socket.io with your FastAPI app 🚀

Topics

Resources

Stars

424 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages