stomp-py
stomp.py is a Python client library for accessing messaging servers such as ActiveMQ, ActiveMQ Artemis, or RabbitMQ using the STOMP protocol (versions 1.0, 1.1, and 1.2). It provides both programmatic access and a command-line client for testing. The library, currently at version 8.2.0, adheres to semantic versioning and exclusively supports Python 3.x, having ended Python 2.x support in January 2020.
Warnings
- breaking stomp.py officially ended support for Python 2.x as of January 2020. All versions 4.2+ are Python 3.x only. Attempting to use newer versions with Python 2.x will result in compatibility errors.
- breaking In version 8.2.0, the `stomp.__version__` attribute changed from a tuple to a string. Tools or scripts that parsed `stomp.__version__` as a tuple for version checks will break.
- gotcha When configuring SSL, `conn.set_ssl()` must be called *before* `conn.start()`. Additionally, use `ssl.PROTOCOL_TLS` instead of deprecated version-specific protocols like `ssl.PROTOCOL_TLSv1_2` for broader compatibility.
- gotcha By default, `stomp.Connection()` might negotiate STOMP 1.1. If you explicitly require STOMP 1.2 features, it's recommended to use `stomp.Connection12()` or specify `headers={'accept-version': '1.2'}` in `conn.connect()` to ensure the correct protocol version is used.
- gotcha Persistent disconnections or connection failures can be caused by aggressive reconnection strategies or heartbeat timeouts. The default reconnection attempts (1 per second for 30 seconds) might be too short for some brokers.
- deprecated Older versions (prior to v2, circa 2013) of stomp.py were distributed as a single Python file (`stomp.py`). Modern versions are installed as a module. Direct imports of `stomp.py` as a file will fail.
Install
-
pip install stomp-py
Imports
- Connection
import stomp conn = stomp.Connection()
- ConnectionListener
import stomp class MyListener(stomp.ConnectionListener): ...
Quickstart
import os
import time
import stomp
class MyListener(stomp.ConnectionListener):
def on_error(self, headers, body):
print(f'ERROR: {body}')
def on_message(self, headers, body):
print(f'MESSAGE: {body}')
host = os.environ.get('STOMP_HOST', 'localhost')
port = int(os.environ.get('STOMP_PORT', '61613'))
username = os.environ.get('STOMP_USERNAME', 'guest')
password = os.environ.get('STOMP_PASSWORD', 'guest')
destination = os.environ.get('STOMP_DESTINATION', '/queue/test')
host_and_ports = [(host, port)]
try:
conn = stomp.Connection(host_and_ports)
conn.set_listener(MyListener())
conn.connect(username, password, wait=True, headers={'accept-version': '1.2', 'heart-beat': '10000,10000'})
print(f"Connected to {host}:{port}")
conn.subscribe(destination=destination, id=1, ack='auto')
print(f"Subscribed to {destination}")
print(f"Sending message to {destination}")
conn.send(body='Hello, STOMP!', destination=destination)
time.sleep(2) # Give time for message to be received
conn.disconnect()
print("Disconnected.")
except stomp.exception.ConnectFailedException as e:
print(f"Failed to connect: {e}")
except Exception as e:
print(f"An error occurred: {e}")