In my project, I'm using pika/RabbitMQ to send a SQL script from my web page to a remote database. This database processes and returns the conclusion. My problem is that I want to give my users the possibility to use multiple tabs to connect to different databases simultaneously to expedite remote maintenance.
However, I'm facing a significant challenge when attempting to use Django's local storage and session storage. Since each tab shares the same session, changing the session affects everyone. I'm using a control inside my consumer to manage Rabbit consumers individually and to stop only the one that received a response, or to force a consumer to stop if the user refreshes the page. I'm open to any suggestions for a solution. Should I use websockets? Try generating an ID on the client side for control? Any other good solutions?
This is my consumer code:
import pika
from comandos.connectionAMQP.connectionSettings import getConnectionRabbitMQ
class MessageReturn:
def __init__(self, consumer_key) -> None:
self.consumer_key = consumer_key
self.message_body = None
self.is_consuming = True
def set_message_body(self, body):
self.message_body = body
def get_message_body(self):
return self.message_body
def stop_consuming(self):
self.is_consuming = False
# Lista para armazenar instâncias de MessageReturn
consumers = []
def create_consumer(consumer_key):
global consumers
consumer = MessageReturn(consumer_key)
consumers.append(consumer)
return consumer
def stop_consume(consumer_key):
global consumers
for consumer in consumers:
if consumer.consumer_key == consumer_key and consumer.is_consuming:
consumer.stop_consuming()
print(f"Consumption stopped for consumer {consumer_key}.")
break
else:
print(f"No active consumption to stop for consumer {consumer_key}.")
def consume(consumer_key):
connection = pika.BlockingConnection(getConnectionRabbitMQ())
channel = connection.channel()
def callback(ch, method, properties, body):
for consumer in consumers:
if consumer.consumer_key == consumer_key:
consumer.set_message_body(body.decode('utf-8'))
consumer.stop_consuming()
global consumers
state = create_consumer(consumer_key)
channel.basic_consume(queue='Response_BKP_00788556-af71-4c99-a8c8-1fde8841e1ae', on_message_callback=callback, auto_ack=True)
print(f'[*] Waiting for messages for consumer {consumer_key}. To exit press CTRL+C')
while state.is_consuming:
connection.process_data_events()
connection.close()
return state.get_message_body()
I call my consumer with a generated UUID for control, and use this unique ID to enforce a stop if the user refreshes the page. However, I'm facing difficulties with using local storage, as it keeps replacing my UUID.
I'm a desktop developer, so web dev is new to me and I'm using Django for the first time, so I'm a bit lost.