Skip to content

Conversation

micheal-parks
Copy link
Member

@micheal-parks micheal-parks commented Aug 25, 2025

Overview

Currently the only way to connect to a robot with good backoff logic and alternating dial methods is by calling createRobotClient. This function is async, and in its body both creates a RobotClient class and attempts connection (hence the async part) before returning the client.

const client = await createRobotClient(config)

This is problematic for the following reasons:

  • Aborting the initial connection is much more difficult and error prone than disconnecting after the first successful connection because the client instance is not made immediately available. Currently the only way to abort the initial connection attempt is by passing an object, named an "abort controller" (which is not a true AbortController), and mutating it. This approach is fragile and can easily break under typical immutability patterns.
  • It's impossible to emit specific dialing events to surface to users because the logic is in plain functions and are not methods of an EventDispatcher class.

Solution

This PR moves all of the dial logic into the RobotClient class. This is not a breaking change, the behavior of createRobotClient and RobotClient is the same. However, it is now possible to instantiate a robotClient before dialing, making disconnection much easier:

const client = new RobotClient()

client.dial(config).then(() => {
  console.log("connected")
})

// This will disconnect during the first connection attempt without the need for mutating config.
client.disconnect()

Additionally, since the dial logic is now within the RobotClient class, we can emit events that can be piped to a UI or console to provide users more understanding of what's currently happening:

client.on('dialing', (event) => {
  console.log(event.method) // webrtc or grpc
  console.log(event.attempt) // attempt number
})

@micheal-parks
Copy link
Member Author

micheal-parks commented Aug 25, 2025

Setting as a draft to get a viz team review first.

update: I think this is ready.

@micheal-parks micheal-parks requested a review from DTCurrie August 25, 2025 21:39
@micheal-parks micheal-parks marked this pull request as ready for review August 25, 2025 21:40
@micheal-parks micheal-parks requested a review from a team as a code owner August 25, 2025 21:40
@micheal-parks micheal-parks changed the title Move dial logic into RobotClient class [APP-9296] Move dial logic into RobotClient class Aug 26, 2025
@micheal-parks micheal-parks merged commit 64b9308 into viamrobotics:main Aug 26, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants