Skip to main content

Request and response RPC

RPC is for operations where a caller needs one correlated response: query status, move a motor, update configuration, or invoke a tool. MAGPIE keeps the requester/responder model consistent across ZeroMQ, MQTT, and WebRTC.

Create a responder​

from luxai.magpie.transport import ZMQRpcResponder

def handle(request):
return {"status": "ok", "echo": request}

server = ZMQRpcResponder("tcp://*:5556")
try:
while True:
server.respond(handler=handle, timeout=1.0)
finally:
server.close()

Call the service​

from luxai.magpie.transport import ZMQRpcRequester

client = ZMQRpcRequester("tcp://127.0.0.1:5556")
try:
response = client.call({"action": "move", "x": 1.0}, timeout=5.0)
print(response)
except TimeoutError:
print("The service did not reply in time")
finally:
client.close()

Timeouts and retries​

A timeout means the requester cannot prove that the operation failed. The responder may have received and executed the request before the reply was lost. Design mutating operations with an application request ID or another idempotency mechanism before retrying automatically.

Use separate timeout budgets for connection setup, acknowledgement, and long-running work when the transport exposes them. A service that legitimately takes minutes should normally return a job identifier and expose progress through streaming or a status method.

When to add a schema​

Raw request objects are suitable for small internal services. Add JsonRpcSchema when you need named methods, generated tool descriptions, structured errors, or a contract shared with AI clients. Continue with Schema-based RPC.