Skip to main content
Version: 3.5.x

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_force keepalive 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

  1. Open a WebSocket to ws://localhost:10001. The service immediately pushes a full-state frame listing connected devices.
  2. On the first frame, pick the first Inverse3's device_id and build a request message with two parts:
    • session.configure.profile.name — registers the simulation with Haply Hub.
    • A per-device set_cursor_force command with a zero vector. The service uses this as a keepalive — it keeps emitting state frames as long as commands keep arriving.
  3. Send the message back. Strip the session field before the next tick — session profile is a one-shot handshake; subsequent ticks send only the command.
  4. Each subsequent state frame: print the cursor vec3 fields (position, velocity, force), throttled to ~10 Hz, and resend the zero-force keepalive.

Parameters

NameDefaultPurpose
URIws://localhost:10001Simulation channel WebSocket URL
PRINT_EVERY_MS100Console-output throttle
Session profile nameco.haply.inverse.tutorials:print-inverse3Identifies this simulation in Haply Hub

State fields read

From data.inverse3[0].state:

  • cursor_position, cursor_velocity, current_cursor_forcevec3 each

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).

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

Source: Python · C++ · C++ Glaze

Related: WebSocket Protocol · Control Commands (set_cursor_force) · Sessions · Types (vec3)