Connect
Establish a basic client connection to the KubeMQ server using the Python SDK and verify it succeeds.
Overview
Every KubeMQ application starts the same way: open a connection to the broker and prove it actually works before building anything on top of it. This tutorial is that first lesson — create a client, give it a stable identity, and confirm connectivity with a health check, so the pattern is muscle memory before you move on to real messaging.
AsyncQueuesClient is constructed with an address and a client_id — the ID tags this connection in broker logs, subscriptions, and management views, so pick something stable rather than a random string. Using it as an async with context manager calls connect() on entry and close() on exit, so cleanup happens automatically even on error. ping() verifies the round trip cheaply: it returns live server info instead of just "no error," proving the client is talking to a real broker rather than silently misconfigured.
Gotchas: a successful construction doesn't always mean the broker is reachable — connection can happen lazily, so ping() is the only reliable proof; reusing the same client ID across running instances causes routing confusion on the broker; and forgetting the async with block (or an equivalent explicit close()) in quick scripts is a common source of leaked connections under load.
Prerequisites
- KubeMQ server running on
localhost:50000 - Python SDK installed (
pip install kubemq)
Code
"""Example: Basic connection — connect to KubeMQ and verify with ping."""
from __future__ import annotations
import asyncio
from kubemq import AsyncQueuesClient
async def main() -> None:
async with AsyncQueuesClient(
address="localhost:50000", # TODO: Replace with your KubeMQ server address
client_id="python-connection-connect-client",
) as client:
server_info = await client.ping()
print(f"Connected to KubeMQ server: {server_info}")
if __name__ == "__main__":
asyncio.run(main())
# Expected output:
# Connected to KubeMQ server: <server-info>
How It Works
AsyncQueuesClient is constructed with an address and a client_id. The async with block calls connect() on entry and close() on exit, so the connection is always cleaned up. ping() sends a lightweight gRPC health-check and returns a ServerInfo object; printing it confirms the connection succeeded and shows the server host and version.
Related
Was this page helpful?