01. Print Inverse3
Connects to the simulation WebSocket and streams cursor position, velocity, and force from the first Inverse3 the service reports.
What you'll learn:
- Opening a WebSocket connection and receiving the initial full-state message
- Sending a zero-force
set_cursor_forcekeepalive to keep the session ticking - Registering a session profile so Haply Hub recognises your simulation
- The first-message-only handshake pattern — strip session/configure after the first send
- Throttling console output to a readable rate
Workflow
- Open a WebSocket to
ws://localhost:10001. The service immediately pushes a full-state frame listing connected devices. - On the first frame, pick the first Inverse3's
device_idand build a request message with two parts:session.configure.profile.name— registers the simulation with Haply Hub.- A per-device
set_cursor_forcecommand with a zero vector. The service uses this as a keepalive — it keeps emitting state frames as long as commands keep arriving.
- Send the message back. Strip the
sessionfield before the next tick — session profile is a one-shot handshake; subsequent ticks send only the command. - Each subsequent state frame: print the cursor
vec3fields (position, velocity, force), throttled to ~10 Hz, and resend the zero-force keepalive.
Parameters
| Name | Default | Purpose |
|---|---|---|
URI | ws://localhost:10001 | Simulation channel WebSocket URL |
PRINT_EVERY_MS | 100 | Console-output throttle |
| Session profile name | co.haply.inverse.tutorials:print-inverse3 | Identifies this simulation in Haply Hub |
State fields read
From data.inverse3[0].state:
cursor_position,cursor_velocity,current_cursor_force—vec3each
Send / receive
The WebSocket loop: receive a state frame, build and send back a command frame. The first command frame carries the session handshake and a zero-force set_cursor_force keepalive; every subsequent frame carries only the keepalive (session is stripped).
- Python
- C++ (nlohmann)
- C++ (Glaze)
Single async loop — recv() → build command → send() → repeat.
async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)
if first_message:
first_message = False
device_id = data["inverse3"][0]["device_id"]
request_msg = {
"session": {"configure": {"profile": {
"name": "co.haply.inverse.tutorials:print-inverse3"}}},
"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": 0.0, "z": 0.0}}},
}]
}
await websocket.send(json.dumps(request_msg))
request_msg.pop("session", None) # one-shot handshake
libhv drives the WebSocket on its own I/O thread — the per-frame work lives in ws.onmessage. The main thread just blocks on ENTER.
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (first_message) {
first_message = false;
device_id = data["inverse3"][0].at("device_id").get<std::string>();
request_msg = {
{"session", {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:print-inverse3"}}}}}}},
{"inverse3", json::array({
{{"device_id", device_id},
{"commands", {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", 0.0}, {"z", 0.0}}}}}}}},
})},
};
}
ws.send(request_msg.dump());
request_msg.erase("session"); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
Same libhv callback model as the nlohmann variant — only the body changes. Glaze uses compile-time reflection: declare structs that mirror the JSON shape, call glz::read / glz::write_json. std::optional<session_cmd> carries the one-shot handshake; when unset, Glaze omits the field from the serialized JSON.
// Struct models
struct vec3 { float x{}, y{}, z{}; };
struct inverse_state {
vec3 cursor_position{}, cursor_velocity{}, current_cursor_force{};
/* + body_orientation, angular_position, angular_velocity */
};
struct inverse_device { std::string device_id; inverse_state state; };
struct devices_message { std::vector<inverse_device> inverse3; };
struct set_cursor_force_cmd { vec3 vector; };
struct commands_message {
std::optional<session_cmd> session; // omitted from JSON when unset
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
commands_message out_cmds{};
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = print-inverse3 */ };
}
// ... populate out_cmds.inverse3 with zero-force keepalive ...
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
Source: Python · C++ · C++ Glaze
Related: WebSocket Protocol · Control Commands (set_cursor_force) · Sessions · Types (vec3)