Package: rhubarb-py
RhubarbRhubarb is a library that simplifies realtime streaming of events for a number of backends in to a single API. Currently supportsPostgres,kafka,RabbitMQ,redisas well as an internal memory backend useful for testing.InstallationThere are a number of backends that can be used with Rhubarb:KafkaPostgresRedisRabbitMQpip install rhubarb-py[kafka]pip install rhubarb-py[postgres]pip install rhubarb-py[redis]pip install rhubarb-py[rabbitmq]BackendsRhubarb("redis://localhost:6379/0")Rhubarb("kafka://localhost:9092")Rhubarb("postgres://postgres:postgres@localhost:5432/rhubarb")Rhubarb("amqp://guest:guest@localhost/")Rhubarb("memory://")Quick startSimple event consumerasyncwithRhubarb("redis://localhost:6379/0")asevents:asyncwithevents.subscribe(channel="CHATROOM")assubscriber:asyncforeventinsubscriber:awaitwebsocket.send_text(event.message)Simple event producerasyncwithRhubarb("redis://localhost:6379/0")asevents:awaitevents.publish("test message")History retrievalasyncwithRhubarb("redis://localhost:6379/0")asevents:asyncwithevents.subscribe(channel="CHATROOM",history=10)assubscriber:# read the last 10 events published to the channelasyncforeventinsubscriber:awaitwebsocket.send_text(event.message)Custom serializer & deserializerasyncwithRhubarb("redis://localhost:6379/0",serializer=json.dumps,deserializer=json.loads)asevents:asyncwithevents.subscribe(channel="CHATROOM",history=10)assubscriber:# read the last 10 events published to the channelasyncforeventinsubscriber:awaitwebsocket.send_text(event.message)Group subscribing (at-most-once processing)asyncwithRhubarb("redis://localhost:6379/0",serializer=json.dumps,deserializer=json.loads)asevents:asyncwithevents.subscribe("TEST-GROUP-CHANNEL",group_name="TEST_GROUP",consumer_name="sub_1")assubscriber_1:asyncforeventinsubscriber:awaitprocess_job(event)ExampleA minimal working example can be found inexampledirectory.importosfromstarlette.applicationsimportStarlettefromstarlette.concurrencyimportrun_until_first_completefromstarlette.responsesimportHTMLResponsefromstarlette.routingimportRoute,WebSocketRoutefromrhubarbimportRhubarbURL=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>"""asyncdefhomepage(_):returnHTMLResponse(html)asyncdefroom_consumer(websocket):asyncformessageinwebsocket.iter_text():awaitevents.publish(channel="chatroom",message=message)asyncdefroom_producer(websocket):asyncwithevents.subscribe(channel="chatroom")assubscriber:asyncforeventinsubscriber:awaitwebsocket.send_text(event.message)asyncdefws(websocket):awaitwebsocket.accept()awaitrun_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],)🛡 LicenseThis project is licensed under the terms of theMITlicense. SeeLICENSEfor more details.