FastAPI Pub/Sub
A simple but yet elegant publish/subscribe socket implementation using FastAPI Websocket.
Installation
pip install fastapi-pubsub
How it works
FastAPI provides an awesome implementation of Websockets. But it can be painful to configure it to work as a publish/subscribe mechanism.
FastAPI Pub/Sub implemented a BasePubSubWebSocket that handles all client's connections under the hood. All you have to do is extend this class, overriding the method generate_object_to_send(self) with your desired logic.
from fastapi_pubsub import BasePubSubWebSocket
class SimplePubSubWebsocket(BasePubSubWebSocket):
async def generate_object_to_send(self):
# any logic that you want to implement to send to connected users
connections = self.connection_manager.get(self.websocket_id)
return {"num_connected_clients": len(connections)}
app = FastAPI()
@app.websocket("/ws/connected_clients")
async def online_players_endpoint(websocket: WebSocket):
ssw = SimplePubSubWebsocket(websocket, 'connected_clients')
await ssw.run()
BasePubSubWebSocket constructor parameters
| Parameter | Mandatory | Default | Description |
|---|---|---|---|
| websocket | yes | - | the fastapi websocket instance |
| channel_id | yes | - | a custom unique id that identifies that operation. used by connection manager to group clients connected in the same channel |
| redis_credentials | no | None. if not passed, uses the dict in-memory implementation | redis host, port, username, password and db configuration |
A minimum example of a server/client communication can be seen here. Follow the instructions to see it working.
Contributing
Despite being full functional, this project is in a very early stage. Pull requests are very welcome!
Metadata
Release files for fastapi-pubsub 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_pubsub-0.2.0.tar.gz | 3.3 kB | Without provenance | Details for fastapi_pubsub-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_pubsub-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_pubsub-0.2.0-py3-none-any.whl |
Total release size: 7.1 kB
Release files / fastapi_pubsub-0.2.0.tar.gz
| Download URL | fastapi_pubsub-0.2.0.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe41d5eb62071aeb76b8a348fbbddadf182982b0104463b1ffcdbae1e74c79e3
|
|
BLAKE2b-256 checksum How to use checksums |
c92a056d3054b2096032647a77246867e4594cfc293fb75b39a70ab178c7edae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Release files / fastapi_pubsub-0.2.0-py3-none-any.whl
| Download URL | fastapi_pubsub-0.2.0-py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a13eddc1eb7a64ccb0cbb752a4c56dd4a9e764b9c55a28edb0f2f15666171bb9
|
|
BLAKE2b-256 checksum How to use checksums |
ad82b8ff5022d969d5fd7bb0f48d976d5f7b3c7f90dc39aaaa8f99d8acd55499
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Security for fastapi-pubsub 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.