Simple Example

jasonrbriggs edited this page Sep 14, 2015 · 11 revisions
Clone this wiki locally

A simple example of using stomp.py (assuming running a broker like Apache Apollo on the local machine with the default stomp port - 61613) is:

import time
import sys

import stomp

class MyListener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)
    def on_message(self, headers, message):
        print('received a message "%s"' % message)

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.start()
conn.connect('admin', 'password', wait=True)

conn.subscribe(destination='/queue/test', id=1, ack='auto')

conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')

time.sleep(2)
conn.disconnect()

Assuming you saved this code as "simple.py", you'd run as follows:

$ python simple.py hello world
received a message "hello world"