Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3931505
D2718.id6894.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D2718.id6894.diff
View Options
diff --git a/rabbitmq-flask-client.py b/rabbitmq-flask-client.py
new file mode 100755
--- /dev/null
+++ b/rabbitmq-flask-client.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+from flask import Flask, Response
+import queue
+import os
+import pika
+import sys
+from threading import Thread
+from flask_cors import CORS
+
+app = Flask(__name__)
+cors = CORS(app, resources={r"/*": {"origins": "*"}})
+
+
+class MessageAnnouncer:
+ def __init__(self):
+ self.listeners = []
+
+ def listen(self):
+ q = queue.Queue(maxsize=5)
+ self.listeners.append(q)
+ return q
+
+ def announce(self, msg):
+ for i in reversed(range(len(self.listeners))):
+ try:
+ self.listeners[i].put_nowait(msg)
+ except queue.Full:
+ del self.listeners[i]
+
+
+@app.route("/", methods=["GET"])
+def get_notifications():
+ def stream():
+ messages = announcer.listen()
+ while True:
+ msg = messages.get()
+ yield msg
+
+ return Response(stream(), mimetype="text/event-stream")
+
+
+def listen_notifications():
+ connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
+ channel = connection.channel()
+ channel.queue_declare(queue="hello")
+
+ def on_notification_received(announcer, body):
+ message = body.decode()
+ message = message + "</br>"
+ print(message)
+ announcer.announce(message)
+
+ message = channel.basic_consume(
+ queue="hello",
+ on_message_callback=lambda ch, method, properties, body: on_notification_received(
+ announcer, body
+ ),
+ auto_ack=True,
+ )
+ channel.start_consuming()
+
+
+def app_init():
+ thread = Thread(target=listen_notifications)
+ thread.daemon = True
+ thread.start()
+
+
+announcer = MessageAnnouncer()
+app_init()
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 23, 19:15 (18 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2313038
Default Alt Text
D2718.id6894.diff (1 KB)
Attached To
Mode
D2718: Expose notifications from RabbitMQ as SSE
Attached
Detach File
Event Timeline
Log In to Comment