How to connect my React Native Expo application to IoT Hub and interact? Which package or SDK should I use?

Alex Fang 230 Reputation points
2023-07-12T15:45:47.4766667+00:00

I am attempting to create an app on React Native using Expo Router to figure out how to connect to my IoT Hub. The following is my code in app/index.js.

However, I get the following error when I run npx expo start

Web Bundling failed 3317ms
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.

Can someone help me?

Here, I am trying to connect to the IoT Hub as I load in my home screen.

import { View, Text, Button } from 'react-native';
import React from 'react';
import { Client } from 'azure-iothub';

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');            
	}        
});    

const Home = () => {
    return (
        <View>
            <Text>Using Azure IoT Hub App</Text>
            <Button>connect</Button>
        </View>
    );
}



Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,078 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
202 questions
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 12,111 Reputation points
    2023-07-18T01:18:59.02+00:00

    Hi @Alex Fang Greetings! Apologies for the delayed feedback on this.

    I have investigated further into your use case. I would like to clarify that both azure-iothub or azure/arm-iothub are Node.js packages. They are dependent on Node.js. Please refer the link Azure SDK Node.js support to identify the minimum version needed to run the SDK. Further more, looking at the Azure React Native documentation Expo framework I found that it does not support Node.js by default. This is the reason why you are encountering these errors. Hope this answers your question.

    I have come across this GitHub repository Azure IoT Starter Kit Companion which will help bridge the compatibility issues with React Native application and IoT SDK.

    Here are couple few other possibilities you might consider to achieve what you are looking for:

    • You can explore possibilities of Creating a Node.js and React app in Visual Studio and figure out how to combine the IoT Hub service SDK with the code. Please note that this approach lets you create a React application but not react native app.
    • If mobile application is what you target and you are flexible to choose a different framework, this article How to use the Node.js SDK for Azure Mobile Apps would guide you with the steps needed to use Azure Node.js SDKs in a mobile application.

    Hope this helps. Please let me know if you need any additional assistance or guidance on the above shared approaches.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.


0 additional answers

Sort by: Most helpful