Subscribing to messagesΒΆ

Read the next message from a topic, or stream messages until the subscription is closed.

from olo import Client
from olo.core import TopicInfo
# @olo-playground: autofill-sim-topics

with Client() as client:
    robot = client.robot()
    joint_states = TopicInfo("joint_states", "sensor_msgs/msg/JointState")

    # Block until the next message arrives.
    print(robot.core.get_latest(joint_states))

    # Stream messages until you break or close the subscription.
    with robot.core.subscribe(joint_states) as sub:
        for n, message in enumerate(sub):
            print(f"{n}: {message['header']['stamp']['nanosec']}")
            if n == 10:
                break
import { connect } from "olo/web";
// @olo-playground: autofill-sim-topics

const client = connect();
const robot = await client.robot();
const jointStates = robot.core.topic("joint_states", "sensor_msgs/msg/JointState");

// Block until the next message arrives.
console.log(await robot.core.getLatest(jointStates));

// Stream messages until you break or close the subscription.
const subscription = robot.core.subscribe(jointStates);
let count = 0;
for await (const message of subscription) {
  console.log(`${count}: ${message.header?.stamp?.nanosec}`);
  if (count++ === 10) subscription.close();
}