Skip to main content

Rhubarb is a library that simplifies realtime streaming for a number of backends into a single API

Project description

Rhubarb

Build status Python Version Dependencies Status codecov Documentation Status Code style: black Security: bandit Pre-commit Semantic Versions License

Rhubarb is a library that simplifies realtime streaming of events for a number of backends in to a single API. Currently supports Postgres, kafka, RabbitMQ redis as well as an internal memory backend useful for testing.

Installation

Redis

pip install -U rhubarb-py[redis]

or install with Poetry

poetry add rhubarb-py[redis]

Kafka

pip install -U rhubarb-py[kafka]

or install with Poetry

poetry add rhubarb-py[kafka]

Postgres

pip install -U rhubarb-py[postgres]

or install with Poetry

poetry add rhubarb-py[postgres]

RabbitMQ

pip install -U rhubarb-py[rabbitmq]

or install with Poetry

poetry add rhubarb-py[rabbitmq]

Backends

  • Rhubarb("redis://localhost:6379/0")
  • Rhubarb("kafka://localhost:9092")
  • Rhubarb("postgres://postgres:postgres@localhost:5432/rhubarb")
  • Rhubarb("amqp://guest:guest@localhost/")
  • Rhubarb("memory://")

Example

A minimal working example can be found in example directory.

import os

from starlette.applications import Starlette
from starlette.concurrency import run_until_first_complete
from starlette.responses import HTMLResponse
from starlette.routing import Route, WebSocketRoute

from rhubarb import Rhubarb

URL = os.environ.get("URL", "redis://localhost:6379/0")

events = Rhubarb(URL)

html = """
<!DOCTYPE html>
<html>
    <head>
        <title>Chat</title>
    </head>
    <body>
        <h1>WebSocket Chat</h1>
        <form action="" onsubmit="sendMessage(event)">
            <input type="text" id="messageText" autocomplete="off"/>
            <button>Send</button>
        </form>
        <ul id='messages'>
        </ul>
        <script>
            var ws = new WebSocket("ws://localhost:8000/ws");
            ws.onmessage = function(event) {
                var messages = document.getElementById('messages')
                var message = document.createElement('li')
                var content = document.createTextNode(event.data)
                message.appendChild(content)
                messages.appendChild(message)
            };
            function sendMessage(event) {
                var input = document.getElementById("messageText")
                ws.send(input.value)
                input.value = ''
                event.preventDefault()
            }
        </script>
    </body>
</html>
"""


async def homepage(_):
    return HTMLResponse(html)


async def room_consumer(websocket):
    async for message in websocket.iter_text():
        await events.publish(channel="chatroom", message=message)


async def room_producer(websocket):
    async with events.subscribe(channel="chatroom") as subscriber:
        async for event in subscriber:
            await websocket.send_text(event.message)


async def ws(websocket):
    await websocket.accept()
    await run_until_first_complete(
        (room_consumer, {"websocket": websocket}),
        (room_producer, {"websocket": websocket}),
    )


routes = [
    Route("/", homepage),
    WebSocketRoute("/ws", ws, name="chatroom_ws"),
]


app = Starlette(
    routes=routes,
    on_startup=[events.connect],
    on_shutdown=[events.disconnect],
)

🛡 License

License

This project is licensed under the terms of the MIT license. See LICENSE for more details.

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

rhubarb-py-1.3.0.tar.gz (13.5 kB view hashes)

Uploaded Source

Built Distribution

rhubarb_py-1.3.0-py3-none-any.whl (14.7 kB view hashes)

Uploaded Python 3

Supported by

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