I am pretty new to using React, React Native, and Expo. I am attempting to create an Expo app that connects to and interacts with Microsoft Azure IoT Hub. The package that I am trying to use is azure-iothub
, which is a IoT Hub Service SDK (in Node.js) that fits the needed functionalities of my app. This is an extension of my discussion from here(Azure IoT Hub device and service SDKs | Microsoft Learn).
To reiterate, when I run a sample as described in the readme within my app using node ...
, it runs fine. However, it I try to incorporate it within my frontend code as such:
import { View, Text, Button } from 'react-native';
import React from 'react';
import { useState, useEffect } from 'react';
// import { DeviceClient } from 'azure-iothub';
// import { Client } from 'azure-iothub';
const Client = require('azure-iothub').Client;
const Home = () => {
const [connected, setConnected] = useState(false);
useEffect(() => {
const connectionString = '...';
const client = Client.fromConnectionString(connectionString);
client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
setConnected(true);
}
});
});
return (
<View>
<Text>Using Azure IoT Hub App</Text>
<Text>{connected}</Text>
</View>
);
}
export default Home;
it fails with the following error:
The package at "node_modules\azure-iothub\dist\client.js" attempted to import the Node standard library module "https".
It failed because the native React runtime does not include the Node standard library.
So I am now trying to incorporate it in a folder called server/backend with express or as functions that I just export from a folder called services. Any tips on this?
However, in the discussion link that I shared above, someone said that “the azure-iothub
SDK is designed specifically for Node.js and unfortunately, it is not compatible with React Native”.
I don’t quite understand this. Doesn’t React Native use Node.js? And can’t you use Node.js as backend anyways?
I would appreciate any insight into this. I am honestly very confused.