Hooks
Last updated
Last updated
The useTelemetry
hook makes it very easy to just listen to telemetry on a single device.
Argument | Description | Type | Required? |
---|---|---|---|
HookOptions
key | Description | value type | required |
---|---|---|---|
This hook returns a single value. Every time the device sends new telemetry with key telemetryKey
this hook will update this value.
This hook relies on the assumption that your Device-to-Cloud messages are JSON documents where the key is the telemetry key and the value is the current telemetry value. We plan to support more complex payloads in the future (selecting using JSON Path, Avro, etc). If you have other message payloads, you can use the useD2CMessage hook.
A component subscribing to the temperature
telemetry.
The D2C messages are expected to look like this:
The useMultiTelemetry
hook is a more sophisticated variant of useTelemetry
. It is designed to cover use-cases where a several streams of telemetry of multiple devices need to be subscribed to.
Do not try to perform subscription updates over the initialSubscribers
object. This object is meant solely as an option for use cases where you always have an initial set of subscribers. Updates to initialSubscribers
will not trigger updates in the hook.
This hook returns an object containing other objects and functions to interact with telemetry subscriptions.
The useDirectMethod
hook returns a function that, when invoked, calls a direct method on the target device. It returns a Promise that resolves to the direct method result that the device returns (or an error when the direct method could not be executed, e.g. if the device is offline).
Hook Options
This hook returns a function: (params: Record<string, unknown>) => Promise<IotHubResponse | void>
IoTHubResponse
You pass the method payload to this function and it outputs the result of the direct method invocation on the device.
Do not confuse the onGrantError
handler of useDirectMethod
with the catch block of the direct method itself. onGrantError
will only be executed when this hook is rendered by react and the security backend fails to grant the direct method. The error that reboot
may throw is an HTTP error that could occur when the function is executed.
The useDeviceTwin
hook subscribes to device twin updates.
Hook Options
This hook returns a value: Twin
This hook returns the device twin whenever twin updates are made on the IoTHub device. This hook uses the Twin
typescript type provided from the azure-iothub library.
Here is an example of a returned device twin:
The useConnectionState
hook subscribes to the connection state of a device. This state can change, i.e as soon as a device connects or disconnects.
Hook Options
This hook returns a value: boolean
This value changed as soon as a device connects or disconnects.
Do not try to perform subscription updates over the initialSubscribers
object. This object is meant solely as an option for use cases where you always have an initial set of subscribers. Updates to initialSubscribers
will not trigger updates in the hook.
This hook returns an object containing other objects and functions to interact with telemetry subscriptions.
The usePatchDesiredProperties
hook is used to perform desired property updates on devices.
Hook Options
This hook returns a function: (desiredProperties: Record<string, unknown>) => Promise<IoTHubResponse | void>
IoTHubResponse
The hook takes in an object of desired properties to send to the device with the specified device ID.
When you call the function returned by this hook you will inevitably perform a device twin update. This means you will receive an update of the output of useDeviceTwin
Hook Options
This hook returns the generic type you specified: T
Messages received over this hook have the type unknown
first and are then casted to your type T
. Omitting this generic type will leave the type unknown
.
We assume that every message a device sends will be an object. The return value of this hook will always be the last message sent by the device.
The hooks provided by ux4iot-react are using a specific authorization mechanism. They are designed to provide the easiest API to cover your use-case and hide the most complexity possible. There are two callbacks that are available on (almost) every hook.
onData
A convenient callback to have a custom callback whenever data is received. You will receive updates of subscription hooks mostly over the return value. However, if you want to use custom logic whenever an update is received you would need to use custom hooks to listen for these changes like this:
Therefore onData
as function in subscription hooks, removes the burden of you needing to decide when to notice value changes.
onGrantError
This callback exists on every hook. The purpose of this callback is to inform you about errors that the custom grantRequestFunction
returns. The grantRequestFunction
is something that you need to implement yourself when you want to use ux4iot in production. The purpose of this function is to determine whether you as a user of the react application have the permission to subscribe to telemetry / device twin / connection state or perform a direct method / desired property patch.
Read more about this here.
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Key | Value | Type |
---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Key | Value | Type |
---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
Argument | Description | Type | Required? |
---|---|---|---|
deviceId
The device ID of the device from which to receive telemetry data
string
telemetryKey
The key of the telemetry item
string
options
Configuration Options
HookOptions
onData
Callback, executed when new telemetry of telemetryKey
is received on the device
(data: unknown) => void
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the direct method request
GrantErrorCallback
onSubscriptionError
Callback, executed when an error is returned from the subscription endpoints of ux4iot
SubscriptionErrorCallback
options
Configuration Options
HookOptions
initialSubscribers
Object of key-value pairs, with keys: the device IDs of your IoTHub devices, and value: a list of strings, defining the telemetryKeys to subscribe to
Record<string, string[]>
onData
Callback, executed when a new value
for a telemetryKey
is sent by a device with ID deviceId
(deviceId: string, telemetryKey: string, value: unknown) => void
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the subscription request.
GrantErrorCallback
onSubscriptionError
Callback, executed when an error is returned from the subscription endpoints of ux4iot
SubscriptionErrorCallback
telemetry
Object holding the current values of all your telemetry subscriptions
Record<string, Record<string, unknown>
toggleTelemetry
Toggles a telemetry subscription for a deviceId
and telemetryKey
(deviceId: string, telemetryKey: string) => void
addTelemetry
Adds a telemetry subscription for a deviceId
and multiple telemetryKeys
(deviceId: string, telemetryKeys: string[]) => void
removeTelemetry
Removes a telemetry subscription for a deviceId
and multiple telemetryKeys
(deviceId: string, telemetryKeys: string[]) => void
isSubscribed
Checks whether a telemetry subscription for a deviceId
and telemetryKey
exists
(deviceId: string, telemetryKey: string) => boolean
currentSubscribers
Object containing all current subscribers with key being the deviceId
and value being the telemetryKey names.
Record<string, string[]>
deviceId
The device ID of the device to execute the direct method on
string
methodName
The name of the method to execute on the device
string
options
Configuration Options
HookOptions
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the direct method request
GrantErrorCallback
deviceId
The device id of the device you want to subscribe to.
string
options
Configuration Options
HookOptions
onData
Callback, executed when a new twin updated is received.
(twin: Twin) => void
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the subscription request.
GrantErrorCallback
onSubscriptionError
Callback, executed when an error is returned from the subscription endpoints of ux4iot
SubscriptionErrorCallback
deviceId
The device id of the device you want to subscribe to.
string
options
Configuration Options
HookOptions
onData
Callback, executed when a new connectionState update is received.
(connectionState: boolean) => void
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the subscription request.
GrantErrorCallback
onSubscriptionError
Callback, executed when an error is returned from the subscription endpoints of ux4iot
SubscriptionErrorCallback
options
Configuration Options
HookOptions
initialSubscribers
Object of key-value pairs, with keys: the device IDs of your IoTHub devices, and value: a list of strings, defining the telemetryKeys to subscribe to
Record<string, string[]>
onData
Callback, executed when a new value
for a telemetryKey
is sent by a device with ID deviceId
(deviceId: string, telemetryKey: string, value: unknown) => void
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the subscription request.
GrantErrorCallback
onSubscriptionError
Callback, executed when an error is returned from the subscription endpoints of ux4iot
SubscriptionErrorCallback
connectionStates
Object holding the current values of all your connectionState subscriptions
Record<string, boolean>
toggleConnectionState
Toggles a connectionState subscription for a deviceId
(deviceId: string) => Promise<void>
addConnectionState
Adds a connectionState subscription for a deviceId
(deviceId: string) => Promise<void>
removeConnectionState
Removes a connectionState subscription for a deviceId
(deviceId: string) => Promise<void>
isSubscribed
Checks whether a telemetry subscription for a deviceId
exists
(deviceId: string) => boolean
currentSubscribers
List of all current deviceIds that are subscribed
string[]
deviceId
The device id of the device onto which to patch the desired properties
string
options
Configuration Options
HookOptions
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the patch desired properties request.
GrantErrorCallback
deviceId
The device ID of the device you want to subscribe to.
string
options
Configuration Options
HookOptions
onData
Callback, executed when the device sends a new message.
(data: Record<string, unknown>) => void
onGrantError
Callback, executed when the grantRequestFunction
fails to grant the subscription request.
GrantErrorCallback
onSubscriptionError
Callback, executed when an error is returned from the subscription endpoints of ux4iot
SubscriptionErrorCallback