Skip to content

How to Manage Subscriptions Dynamically

This guide shows you how to add and remove data subscriptions at runtime without reconnecting. This is useful when your UI changes (e.g., user opens an engine panel) and you need different data.

Why Use Dynamic Subscriptions?

Instead of subscribing to all fields upfront (which uses more bandwidth), you can:

  • Start with minimal data
  • Add fields when the user needs them
  • Remove fields when they're no longer displayed

Check Current Subscriptions

See what fields you're currently receiving:

javascript
const currentFields = listener.getFields();
console.log(currentFields);
// ['indicated_altitude', 'indicated_airspeed', 'true_heading']

Add More Fields

Subscribe to additional fields without reconnecting:

javascript
await listener.subscribe(['fuel_quantity_gallons', 'eng_rpm_1', 'eng_rpm_2']);

New fields will start appearing in your update events immediately.

Remove Fields

Unsubscribe from fields you no longer need:

javascript
await listener.unsubscribe(['true_heading']);

Example: Tabbed Dashboard

Here's a practical example with tabs that load different data:

javascript
const tabs = {
  flight: ['indicated_altitude', 'indicated_airspeed', 'true_heading'],
  engine: ['eng_rpm_1', 'eng_rpm_2', 'fuel_quantity_gallons', 'fuel_flow_gph_1'],
  autopilot: ['autopilot_master', 'autopilot_altitude_set', 'autopilot_heading_set']
};

let currentTab = 'flight';

async function switchTab(newTab) {
  // Unsubscribe from current tab's fields
  await listener.unsubscribe(tabs[currentTab]);

  // Subscribe to new tab's fields
  await listener.subscribe(tabs[newTab]);

  currentTab = newTab;
  updateUI();
}

Example: On-Demand Engine Monitoring

Only subscribe to engine data when the user expands that section:

javascript
let engineExpanded = false;

async function toggleEnginePanel() {
  if (engineExpanded) {
    await listener.unsubscribe([
      'eng_rpm_1', 'eng_rpm_2',
      'eng_throttle_1', 'eng_throttle_2',
      'fuel_flow_gph_1', 'fuel_flow_gph_2'
    ]);
    hideEnginePanel();
  } else {
    await listener.subscribe([
      'eng_rpm_1', 'eng_rpm_2',
      'eng_throttle_1', 'eng_throttle_2',
      'fuel_flow_gph_1', 'fuel_flow_gph_2'
    ]);
    showEnginePanel();
  }

  engineExpanded = !engineExpanded;
}

Best Practices

Start minimal: Only subscribe to what you need on initial load.

javascript
// Good - only essential fields
const listener = await ggClient.createListener({
  userId: userId,
  gameId: 'msfs',
  fields: ['indicated_altitude', 'indicated_airspeed']
});

// Less efficient - subscribes to everything
const listener = await ggClient.createListener({
  userId: userId,
  gameId: 'msfs'
  // No fields = all fields
});

Batch changes: If adding multiple fields, do it in one call:

javascript
// Good - single call
await listener.subscribe(['field1', 'field2', 'field3']);

// Less efficient - multiple calls
await listener.subscribe(['field1']);
await listener.subscribe(['field2']);
await listener.subscribe(['field3']);

Available Fields

See all fields you can subscribe to: