mxcubecore.ipc.transports.nanomq

IPC transport over MQTT, for use with a NanoMQ broker (or any MQTT 3.1.1/5-compliant broker. We are using the paho-mqtt client).

MQTT itself has no notion of “a connection” the way a TCP socket does, so single-client enforcement happens purely at the message layer: every message must carry a _client_id, and the first one to authenticate claims the session until a message announcing that same _client_id arrives on the <topic_prefix>/disconnect topic.

A well-behaved client publishes that message itself right before it disconnects cleanly, and also registers it as an MQTT Last Will (client.will_set(…)) so the broker publishes it on the client’s behalf if it disconnects uncleanly (crash, network loss) see IPC_FORMAT.md section 3. Without either, the slot is only freed when the whole IPCServer/transport is stopped.

Classes

NanoMQTransport(host, port, topic_prefix[, ...])

Single-client MQTT transport for a NanoMQ (or compatible) broker.

class mxcubecore.ipc.transports.nanomq.NanoMQTransport(host: str, port: int, topic_prefix: str, tls_ca_cert_path: str | None = None)[source]

Bases: Transport

Single-client MQTT transport for a NanoMQ (or compatible) broker.

Parameters:
  • host (str)

  • port (int)

  • topic_prefix (str)

  • tls_ca_cert_path (str | None)

bind(session_id: str) → None[source]

Called by IPCServer once session_id has authenticated.

Transports that can’t tell clients apart by connection (NanoMQ) use this to reserve the single client slot.

Parameters:

session_id (str)

Return type:

None

has_active_client() → bool[source]

Whether a client is currently connected.

Return type:

bool

send(session_id: str, raw_json: str) → None[source]

Send one frame to the given session, if it is still connected.

Parameters:
  • session_id (str)

  • raw_json (str)

Return type:

None

start(on_message: Callable[[str, str], None], on_connect: Callable[[str], None], on_disconnect: Callable[[str], None]) → None[source]

Start listening for (or connecting to) clients.

on_message(session_id, raw_json) is called for every inbound frame.

on_disconnect(session_id) when a client’s connection is torn down at the transport level

on_connect(session_id, peer) is called when a connection is established; peer is a human-readable “ip:port (hostname)” string for transports that have one (JSON-RPC/TCP), or None for transports that don’t (NanoMQ has no per-connection socket only a _client_id in each message).

Parameters:
Return type:

None

stop() → None[source]

Stop the transport and release any resources (sockets, MQTT client loop etc.).

Return type:

None