Skip to main content

Lightweight library for developing real time applications

Project description

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

$ pip install spilo

Here's example of the backend code for a simple websocket server:

server.py

from typing import Dict
from dataclasses import dataclass
from fastapi import FastAPI, WebSocket

from spilo.channel import Channel
from spilo.base_client import BaseClient
from spilo.redis_pubsub import RedisPubSub
from spilo.event_registry import EventRegistry

app = 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.int

    async 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"})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spilo-1.0.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spilo-1.0.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file spilo-1.0.0.tar.gz.

File metadata

  • Download URL: spilo-1.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for spilo-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c298b9014589c2b546abf81f1b421b28cffefee04ffa408b1846a56062ee9c25
MD5 0f9037eda1af8970d9c795d90c21131c
BLAKE2b-256 31caa3b5766b2ef11003c3ad0aa69e52554c67b40de0ab8b055cd74e52a64df6

See more details on using hashes here.

File details

Details for the file spilo-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: spilo-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for spilo-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7deb4448884abfda6aaf994e0c666f4b185779707a3bc9637f16c541a2cc116
MD5 603e5b11ea1dee615775dc6b88016e77
BLAKE2b-256 c15f37584fef87eb385ea3fea211a0f90b3252131f97cb98b46a79842b597714

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page