Skip to content

BabyROS Client/Server Tutorial

Goal: Create and run a Client and Server node using Python.

Level: Beginner

Time: ~15 minutes


Contents

  1. Background
  2. Prerequisites
  3. Tasks
    1. Create a Server Node
    2. Create a Client Node
    3. Run the Nodes
  4. Summary
  5. Next Steps

Background

This tutorial demonstrates a simple request/response pattern using BabyROS. A Server waits for incoming client requests, processes them, and returns a response. A Client sends data to the server and receives a reply.

This pattern is useful for robotics and distributed systems where components need to query one another, e.g., sensor calibration requests, control parameter queries, or cloud-edge communication.


Prerequisites

  • Python 3.8+ installed
  • BabyROS installed in your Python environment
  • Basic understanding of Python dictionaries

Tasks

1. Create a Server Node

Create a file named server_example.py:

python
import time
import babyros

def handle_request(request):
    """
    Example service callback.
    Args:
        request (dict | None): Data sent by the client. May be None if no payload.
    Returns:
        dict: The response sent back to the client.
    """
    if request is None:
        print("Callback: No request payload received.")
        return {"message": "No request received!"}

    print("Request received and processed!")
    return {
        "message": "Hello from server!",
        "received": request
    }

if __name__ == "__main__":
    server = babyros.node.Server("example/topic", handle_request)
    print("Server started successfully!")

    topics = babyros.get_topics_in_session()
    print("Active topics in current session:", topics)

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Shutting down server...")
        server.delete()

2. Create a Client Node

Create a file named client_example.py:

python
import json
import babyros

if __name__ == "__main__":
    client = babyros.node.Client(topic="example/topic")

    topics = babyros.get_topics_in_session()
    print("Active topics in current session:", topics)

    request = {"param1": "value1", "param2": "value2"}
    print("Sending request:", json.dumps(request))

    response = client.request(data=request)

    if not response:
        print("No response received from server.")
    else:
        print("Response:", response[0]["received"])
        print("Request:", request)
        print("Equal?", request == response[0]["received"])

    client.delete()

3. Run the Nodes

Open a terminal and start the server:

bash
python server_example.py

The server will start listening for client requests.

Open a second terminal and run the client:

bash
python client_example.py

You should see the request sent by the client and the server’s response printed in both terminals.

INFO

Press Ctrl+C in both terminals to stop the nodes.

Summary

You have:

  • Created a Server node that processes client requests and returns responses.
  • Created a Client node that sends data and receives replies.
  • Observed real-time Client/Server messaging with BabyROS.

Next Steps

  • Experiment with different request/response data types.
  • Combine with Publisher/Subscriber nodes for hybrid communication.
  • Integrate with real sensors or cloud services for robotics applications.