Using the Resource data client

The ResourceDataClient is a service that can retrieve resource data in an easy and efficient manner.

You can create a client instance using the createResourceDataClient() method on the component context.

const client = context.createResourceDataClient();

To start retrieving resource data (for example the device name) you can use the query() method to set up a listener. What's nice about this is that when the data is updated, the listener will be called again with the latest data.

const client = context.createResourceDataClient();

client.query([
  { selector: 'Agent', fields: ['name'] }
], result => {
  console.log(result);
  // result: [{ data: { name: 'CNC Machine' }, query: { fields: ['name'], selector: 'Agent' } }]
});

When a component's lifecycle ends, the component context is automatically destroyed, effectively calling destroy() on any ResourceDataClient created via that context. This will in turn cancel all query listeners that were set up from these clients.

However, you can choose to manually destroy the client instance or cancel a specific query listener. A use case for the latter may be to stop listening for changes as soon as you receive a first result.

const client = context.createResourceDataClient();

const cancelQuery = client.query([
  { selector: 'MyUser', fields: ['name'] }
], result => {
  console.log('Hello, ' + result[0].data.name + '!');
  // result: "Hello, John Doe!"
  
  // Stops the listener, because the name was needed just once.
  cancelQuery();
});

The next example showcases a query which combines data from two different resources:

const client = context.createResourceDataClient();

client.query([
  { selector: 'Agent', fields: ['custom'] },
  { selector: 'CustomPropertyList', fields: ['name', 'scopeType', 'slug'] }
], results => {
  const agent = results[0].data;
  const customProps = results[1].data;
  const agentCustomProps = customProps.map(prop => ({
    name: prop.name,
    value: agent.custom[prop.scopeType + prop.slug],
  }));
 
  console.log(agentCustomProps);
  // result: [{ name: 'Project number', value: '100' }]
});

Resource selectors

The ResourceDataClient is somewhat limited in the kind of resources and fields it can retrieve. Below you can find the currently supported selectors and their allowed fields.

Selector

Allowed fields

Description

API endpoint used

Agent

activeVpnSession.rscServer.name,
activeVpnSession.rscServer.publicId,
activeVpnSession.vpnAddress,
connectedUsers.name,
connectedUsers.publicId,
custom,
dataSources.name,
dataSources.publicId,
dataSources.slug,
description,
lastSeenAgentUserAgent.firmwareVersion,
lastSeenAgentUserAgent.hardware,
lastSeenAgentUserAgent.hardwareVersion,
location.name,
loggingMdrServer.name,
loggingMdrServer.publicId,
loggingMqttChangedOn,
macAddress,
mdrServer.name,
mdrServer.publicId,
memberships.group.publicId,
memberships.publicId,
mqttChangedOn,
name,
publicId,
rateLimiting.configMqttCommandCounter,
rateLimiting.configMqttCommandLastHit,
rateLimiting.configMqttCommandLimit,
rateLimiting.configMqttDataCounter,
rateLimiting.configMqttDataLastHit,
rateLimiting.configMqttDataLimit,
rateLimiting.configOpenvpnCounter,
rateLimiting.configOpenvpnLastHit,
rateLimiting.configOpenvpnLimit,
serialNumber,
servers.name,
servers.publicId,
servers.type,
type.name,
type.publicId

The current Agent (i.e. the Agent for the Page that is currently open, or the Agent for the Card that the component is placed on).

Agent

CustomPropertyList

flags,
name,
publicId,
resourceType,
scopeType,
slug

All Custom Properties for the current Company.

CustomPropertyList

GroupList

name,
publicId,
type.name,
type.publicId

All Groups in the current Company.

GroupList

MyUser

name,
publicId,
unreadMessagesCount

The currently logged in User.

MyUser

Branding

logo,
theme.logoHasBackgroundColor

The currently active branding.

CompanyBranding

PageVersion

title

The current Page (i.e. the Page that is currently open, or the Card that the component is placed on).

PageVersion

Please reach out to us by sending an e-mail to [email protected], if you are missing items on this list and you would like to use the logging data client to retrieve data for other resource types or allowed fields.


Did this page help you?