Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/giorgikhunashvili/spilo
Lightweight library for developing real time applications
https://github.com/giorgikhunashvili/spilo
pubsub python redis websocket
Last synced: 26 days ago
JSON representation
Lightweight library for developing real time applications
- Host: GitHub
- URL: https://github.com/giorgikhunashvili/spilo
- Owner: GiorgiKhunashvili
- License: mit
- Created: 2022-11-05T00:12:26.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-16T12:21:12.000Z (about 1 year ago)
- Last Synced: 2024-10-10T23:20:10.508Z (26 days ago)
- Topics: pubsub, python, redis, websocket
- Language: Python
- Homepage:
- Size: 55.7 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SPILO
Spilo is lightweight library for developing real time applications which helps developers managing websocket clients effectively and gives ability to scale horizontaly for handling large amount of clients.
## Installation
```console
$ pip install spilo
```Here's example of the backend code for a simple websocket server:
**server.py**
```python
from typing import Dict
from dataclasses import dataclass
from fastapi import FastAPI, WebSocketfrom spilo.channel import Channel
from spilo.base_client import BaseClient
from spilo.redis_pubsub import RedisPubSub
from spilo.event_registry import EventRegistryapp = FastAPI()
redis_pubsub = RedisPubSub()
redis_pubsub.connect()
event_registry = EventRegistry(event_key_name="event_type")@dataclass
class Client(BaseClient):protocol: WebSocket
def __hash__(self):
return self.client_id.intasync def send(self, data):
await self.protocol.send_text(str(data))async def close(self):
await self.protocol.close()async def listen(self):
return await self.protocol.receive_text()@app.websocket("/ws/{channel_name}")
async def websocket_endpoint(websocket: WebSocket, channel_name: str):
await websocket.accept()
client = Client(protocol=websocket)
channel = Channel.get(channel_name, redis_pubsub, event_registry)
channel.add_client(client)
await channel.listen_client(client)@event_registry.on("test")
async def test_event_handler(data: Dict, client: BaseClient, channel: Channel):
await client.send(str(data))
await channel.publish({"event_type": "test", "data": "test_data"})
```