How to Target an Interop Instance When agm Is Undefined in the Main App

In io.Connect Browser, an app instance’s agm property (io.appManager.instances()[i].agm) is filled in once, when the client that reads it first learns about that instance, and is never updated. The Main app (the app that hosts the Browser Platform) learns about each new instance before that instance has connected to io.Connect, so in the Main app every field of agm is undefined. The same happens in any app that was already running when the instance started, Workspace apps included. Only apps started after the instance connected see populated values.

Solution: target the instance by its ID, which invoke() resolves when the call is made:

await io.interop.invoke(methodName, args, { windowId: instance.id });

:warning: Do not pass an empty agm object as the target. Fields set to undefined are ignored when filtering, so an all-undefined agm matches every server that offers the method. The method runs on all of them, and the call reports no error.


Quick Diagnostic Guide

What you see Context Likely Cause Recommended Action
io.appManager.instances()[i].agm is { application: undefined, …, windowId: undefined } Main app, or an app running before the target started Captured before the target connected; not refreshed Target { windowId: instance.id } (Step 2)
agm is fully populated An app started after the target connected Captured after the target connected Use Step 2 anyway: an app started earlier gets undefined
instance.interopInstance is undefined Any context App instances have no such property Use instance.id
invoke(method, args, instance.agm) runs on several instances Wherever agm is empty An all-undefined filter matches every server offering the method Step 2
invoke() rejects with Can not find a method matching "<method>" with server filter {"windowId":"<id>"} Any context No matching server offered the method within waitTimeoutMs (30 000 ms by default) Check the ID, and that the target registers the method (Step 3)

Prerequisites

Access / Tools

  • The DevTools console of the app that makes the call, with its io API reachable there (for example, window.io = io in your app code).

Environment

  • Product: io.Connect Browser (@interopio/browser, @interopio/browser-platform).

Instructions

Step 1: Identify the Symptom

In the Main app’s console, compare what App Management and Interop report for the same instance:

io.appManager.instances().map((i) => ({ id: i.id, agm: i.agm }));
// [{ id: "g42-ofl3PP7yYk",
//    agm: { application: undefined, applicationName: undefined, peerId: undefined,
//           instance: undefined, windowId: undefined } }, …]

io.interop.servers().map((s) => ({ application: s.application, windowId: s.windowId }));
// [{ application: "my-app", windowId: "g42-ofl3PP7yYk" }, …]

Interop knows the instance, and its windowId matches the instance id. Only the agm snapshot is empty. Invoking with that agm while two instances of my-app run succeeds, but result.all_return_values.length is 2.


Step 2: Target the Instance by Its ID

Pass the instance ID as a windowId filter:

/**
 * Invokes an Interop method on exactly one app instance.
 * @param {string} methodName - The Interop method to invoke.
 * @param {object} args - Invocation arguments.
 * @param {string} instanceId - The app instance ID (equal to the app's windowId).
 */
async function invokeOnInstance(methodName, args, instanceId) {
  if (typeof instanceId !== "string" || instanceId === "") {
    // { windowId: undefined } is an empty filter and would match every server.
    throw new Error("invokeOnInstance: instanceId is required");
  }
  return io.interop.invoke(methodName, args, { windowId: instanceId });
}

If no matching server offers the method yet (for example, when you call from onInstanceStarted before the app has connected), invoke() keeps checking for up to waitTimeoutMs, 30 000 ms by default (pass { waitTimeoutMs: <ms> } as the fourth argument to change it). The call goes through once the app registers the method; otherwise invoke() rejects with the error in the Diagnostic Guide.

Alternative: io.interop.servers(). Passing the result of io.interop.servers().find((s) => s.windowId === instanceId) also works once the target has connected. Before that, find() returns undefined, and invoke() with an undefined target falls back to "best": one server chosen by the library, not necessarily the one you meant. Check the lookup result before invoking.


Step 3: Verify the Invocation

  1. Confirm that the target has connected:
io.interop.servers().some((s) => s.windowId === instanceId); // true
  1. Invoke and count the instances that ran the method:
const result = await io.interop.invoke(methodName, args, { windowId: instanceId });
result.all_return_values.length; // 1

A count above 1 means more than one server matched; check that instanceId is set.
3. Confirm the target app handled the call (for example, in its own console or logs).


Under the Hood: Why This Happens

When a client creates its object for an app instance, it copies the instance’s Interop details (application, applicationName, peerId, instance, windowId) into agm from the servers it can see at that moment, and the object is not updated afterwards. Clients create these objects at startup and whenever the Main app announces a new instance, which it does on opening the window or registering the Workspace window, before the app inside has connected. With no server to copy from yet, every field is undefined.

A filter such as { windowId } doesn’t use that snapshot: invoke() matches it against the live list of servers when the call is made, whenever the calling app started.


Best Practices & Multi-Instance Handling

  • Target the instance ID, not the app name. Every instance of an app shares the same application, so { application: "my-app" } matches all of them.
  • Get the ID from what you already have:
    • App Management: instance.id from io.appManager.instances(), from onInstanceStarted, or from the instance application.start() returns.
    • Workspaces: the id of a loaded Workspace window, for example in io.workspaces.onWindowLoaded.
    • The calling app: have it send io.windows.my().id in the request payload.
  • Guard against an empty ID. A Workspace window that hasn’t loaded can have an undefined id, and { windowId: undefined } matches every server.

References