[Django]-Stomp.py how to return message from listener

4๐Ÿ‘

โœ…

Ok, I found a way myself. All you have to do, is a slight change of the listener class:

class MyListener(object):
    msg_list = []

    def __init__(self):
        self.msg_list = []

    def on_error(self, headers, message):
        self.msg_list.append('(ERROR) ' + message)

    def on_message(self, headers, message):
        self.msg_list.append(message)

And in the code, where u use stomp.py:

conn = stomp.Connection()
lst = MyListener()
conn.set_listener('', lst)
conn.start()
conn.connect()
conn.subscribe(destination='/queue/test', id=1, ack='auto')
time.sleep(2)
messages = lst.msg_list
conn.disconnect()
return render(request, 'template.html', {'messages': messages})
๐Ÿ‘คTosh

Leave a comment