{"library":"solace-pubsubplus","title":"Solace PubSub+ Python API","type":"library","description":"The `solace-pubsubplus` library provides a Python API for interacting with Solace PubSub+ event brokers. It supports publishing and subscribing to messages, managing topics, queues, and handling various messaging patterns. Currently at version 1.11.0, it follows a regular release cadence with updates for new features and bug fixes, typically aligning with Solace PubSub+ broker releases.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install solace-pubsubplus"],"cli":null},"imports":["from solace_pubsubplus import SolaceSession","from solace_pubsubplus import Message","from solace_pubsubplus import Subscription","from solace_pubsubplus import SolaceSessionProperties","from solace_pubsubplus import DeliveryMode"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":"https://docs.solace.com/API/Messaging-APIs/Python-API/python-home.htm","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/solace-pubsubplus/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nimport time\nimport threading\nfrom solace_pubsubplus import SolaceSession, SolaceSessionProperties, Message, MessageBuilder, DeliveryMode, Subscription, SubscribeFlags, SolaceLogLevel\n\n# Configuration from environment variables\nSOLACE_HOST = os.environ.get(\"SOLACE_HOST\", \"localhost:55555\") # e.g., 'tcp://your_broker_host:55555'\nSOLACE_VPN = os.environ.get(\"SOLACE_VPN\", \"default\")\nSOLACE_USERNAME = os.environ.get(\"SOLACE_USERNAME\", \"username\")\nSOLACE_PASSWORD = os.environ.get(\"SOLACE_PASSWORD\", \"password\")\n\nTOPIC_NAME = \"python/quickstart/hello\"\n\n# Global lock for thread-safe printing\nprint_lock = threading.Lock()\n\ndef on_message_received(message: Message):\n    \"\"\"Callback function for received messages.\"\"\"\n    with print_lock:\n        print(f\"Received message on topic '{message.get_destination_as_topic()}'\")\n        if message.get_data():\n            print(f\"Message data: {message.get_data().decode('utf-8')}\")\n\ndef run_quickstart():\n    # 1. Configure Solace Session Properties\n    session_properties = SolaceSessionProperties()\n    session_properties.host = SOLACE_HOST\n    session_properties.vpn_name = SOLACE_VPN\n    session_properties.username = SOLACE_USERNAME\n    session_properties.password = SOLACE_PASSWORD\n    session_properties.connect_retries = 3\n    session_properties.reconnect_retries = 3\n\n    # Set log level for more detailed output (optional)\n    SolaceSession.set_log_level(SolaceLogLevel.INFO)\n\n    session = None\n    try:\n        # 2. Create and Connect the Solace Session\n        session = SolaceSession(session_properties)\n        session.connect()\n        with print_lock:\n            print(f\"Connected to Solace PubSub+ broker at {SOLACE_HOST}\")\n\n        # 3. Set up Message Callback\n        session.set_message_receive_callback(on_message_received)\n\n        # 4. Subscribe to a Topic\n        subscription = Subscription(TOPIC_NAME)\n        session.subscribe(subscription, SubscribeFlags.WAIT_FOR_CONFIRM)\n        with print_lock:\n            print(f\"Subscribed to topic '{TOPIC_NAME}'\")\n\n        # 5. Publish a Message\n        message_builder = MessageBuilder()\n        message = message_builder \\\n            .with_delivery_mode(DeliveryMode.DIRECT) \\\n            .with_topic(TOPIC_NAME) \\\n            .with_property(\"user-property\", \"value\") \\\n            .build()\n        message.set_data(f\"Hello from Python Quickstart! Timestamp: {time.time()}\".encode('utf-8'))\n\n        session.publish(message)\n        with print_lock:\n            print(f\"Published message to topic '{TOPIC_NAME}'\")\n\n        # 6. Wait briefly for the message to be received (if publishing to self)\n        time.sleep(2)\n\n    except Exception as e:\n        with print_lock:\n            print(f\"An error occurred: {e}\")\n    finally:\n        if session:\n            # 7. Disconnect the Session\n            with print_lock:\n                print(\"Disconnecting Solace Session...\")\n            session.disconnect()\n            with print_lock:\n                print(\"Disconnected.\")\n\nif __name__ == \"__main__\":\n    run_quickstart()","lang":"python","description":"This quickstart demonstrates how to connect to a Solace PubSub+ broker, subscribe to a topic, publish a direct message, and receive messages using a callback function. Ensure `SOLACE_HOST`, `SOLACE_VPN`, `SOLACE_USERNAME`, and `SOLACE_PASSWORD` environment variables are set or updated in the script.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}