Rhubarb is a library that simplifies realtime streaming for a number of backends into a single API
Project description
Rhubarb
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
This project is licensed under the terms of the MIT
license. See LICENSE for more details.
Project details
Release history Release notifications | RSS feed
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)
Built Distribution
rhubarb_py-1.3.0-py3-none-any.whl
(14.7 kB
view hashes)
Close
Hashes for rhubarb_py-1.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb6dd95a5962a18f72c106d1f80930a40ec1860bfba742d0c7e40e852bffa365 |
|
MD5 | 3b434589efe5120ccf2ae36fec3bbb3a |
|
BLAKE2b-256 | 750e9e0dace843374980930ec10104c79c38ed04c6ea5edc17fa61070f1a958c |