{"id":4408,"library":"ably","title":"Ably Python SDK","description":"The Ably Python SDK provides a client library for the Ably realtime messaging service, enabling developers to build applications with features like Pub/Sub messaging, presence, and message history. Currently at version 3.1.2, it's an actively developed library with frequent patch releases, designed to offer high performance, reliability, and scalability, including support for production AI infrastructure.","status":"active","version":"3.1.2","language":"en","source_language":"en","source_url":"https://github.com/ably/ably-python","tags":["realtime","messaging","websockets","publish-subscribe","asyncio","ai"],"install":[{"cmd":"pip install ably","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The primary asynchronous Realtime client is imported directly from the top-level `ably` package in v3.x. Older patterns might involve submodules.","wrong":"from ably.realtime import AblyRealtime","symbol":"AblyRealtime","correct":"from ably import AblyRealtime"},{"note":"The synchronous REST client is imported directly from the top-level `ably` package in v3.x. Older patterns might involve submodules.","wrong":"from ably.rest import AblyRest","symbol":"AblyRest","correct":"from ably import AblyRest"}],"quickstart":{"code":"import asyncio\nimport os\nfrom ably import AblyRealtime\n\nasync def main():\n    api_key = os.environ.get('ABLY_API_API_KEY', 'your_ably_api_key:YOUR_SECRET_KEY')\n    if api_key == 'your_ably_api_key:YOUR_SECRET_KEY':\n        print(\"WARNING: Replace 'your_ably_api_key:YOUR_SECRET_KEY' with your actual Ably API key or set ABLY_API_API_KEY environment variable.\")\n        print(\"You can obtain a demo key from your Ably dashboard (https://ably.com/dashboard).\")\n\n    async with AblyRealtime(api_key, client_id='my-python-client') as ably_realtime:\n        # Wait for connection to be established\n        await ably_realtime.connection.once_async('connected')\n        print('Connected to Ably!')\n\n        channel = ably_realtime.channels.get('my-test-channel')\n\n        # Subscribe to messages\n        def on_message(message):\n            print(f\"Received message: {message.data}\")\n\n        await channel.subscribe_async(on_message)\n        print(\"Subscribed to 'my-test-channel'\")\n\n        # Publish a message\n        await channel.publish('greeting', 'Hello from Ably Python SDK!')\n        print(\"Published 'Hello from Ably Python SDK!' to 'my-test-channel'\")\n\n        # Keep the connection open for a bit to receive the message\n        await asyncio.sleep(5)\n\n\nif __name__ == '__main__':\n    asyncio.run(main())\n","lang":"python","description":"This quickstart demonstrates how to connect to Ably using the `AblyRealtime` client, subscribe to a channel, and publish a message. It leverages Python's `asyncio` for asynchronous operations. Remember to replace the placeholder API key with your actual Ably API key, ideally retrieved from an environment variable for production applications."},"warnings":[{"fix":"Refer to the official Ably documentation for a detailed migration guide when upgrading from v2 to v3. Update import paths and Realtime client usage according to v3 API.","message":"Version 3.0.0 introduced significant breaking changes, notably adding full Realtime publish support, presence, and mutable messages over WebSockets. This brings Python to feature parity with other SDKs but requires migration for users upgrading from v2.x or earlier, which had limited Realtime capabilities or required an MQTT adapter.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"Always use token authentication in production environments, generated by a trusted backend. If using API keys for development, load them securely from environment variables (e.g., `os.environ`) or a secrets management system.","message":"Using raw API keys directly in your application code, especially in production, is a security risk. API keys in quickstarts are for demonstration purposes only.","severity":"gotcha","affected_versions":"All"},{"fix":"Upgrade to `ably` version `3.1.2` or later to ensure proper preservation of message extras during updates.","message":"Versions of the SDK prior to `3.1.2` (including `2.1.4`) had a bug where message 'extras' (metadata) might not be preserved correctly in message update methods, potentially leading to data loss. [v3.1.2 release notes]","severity":"gotcha","affected_versions":">=2.0.0, <3.1.2"},{"fix":"Ensure you are using `ably` version `3.1.1` or later to benefit from improved WebSocket handling and reconnection stability. If on v2, upgrade to `2.1.4` or later.","message":"Older versions of the SDK (e.g., prior to `3.1.1` and `2.1.4`) contained issues with handling normal WebSocket close frames and improving reconnection logic, which could lead to unexpected connection drops or delayed re-establishment. [v3.1.1 release notes, v2.1.4 release notes]","severity":"gotcha","affected_versions":">=2.0.0, <3.1.1"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}