## 4 IoT Integration with Digital Twin
### Middleware Framework  
Middleware is an essential software layer that facilitates communication and service integration between operating systems, applications, and DTs, while providing critical services (i.e., communication, authentication, API management, processing). By managing data flow and interaction in real-time, middleware enables seamless coordination between physical devices and their virtual counterparts in DT frameworks. This capability is particularly critical in complex environments such as IoT systems, where reliable and scalable distributed operations are required to maintain an accurate and responsive Digital Twin. Middleware thus supports the development of efficient, scalable applications that continuously synchronize real data with digital replicas, enabling real-time analysis, predictive insights, and system optimization.  
As depicted in Figure 5, the MQTT middleware framework in an IoT setup where various constrained devices (e.g., temperature sensors) generate data for applications or other devices is presented. MQTT clients act as publishers and subscribers, while managing this data exchange. In distributed IoT systems, middleware manages protocols and data flows and abstracts hardware complexities to provide a unified interface. MQTT, a widely used middleware solution, uses a publish/subscribe model for efficient data distribution [33]. In this architecture, a sensor can publish data to an MQTT broker, which then distributes it to subscribed clients on specific topics (e.g., topics A, B, or C). Compared to protocols like HTTP or CoAP, MQTT's lightweight design optimizes it for IoT applications by minimizing bandwidth and power consumption, making it ideal for low-resource IoT devices [34].  
MQTT stands out as an effective protocol for real-time, low-latency communication in IoT systems, providing an efficient publish/subscribe mechanism tailored to devices with limited power and bandwidth resources. Unlike HTTP and CoAP, MQTT's lightweight design is optimized for minimal resource consumption, making it particularly suitable for IoT environments [35]. Middleware frameworks that integrate MQTT also facilitate cross-platform communication, particularly in edge computing environments, by enabling local data processing and reducing reliance on centralized networks. As shown, MQTT brokers deployed on edge devices support continuous data exchange even in offline scenarios, improving the reliability and responsiveness of local IoT systems and ensuring that critical functions function regardless of external network conditions.  
Figure 4: Data flow and integration process of HEMS.  
In Figure 6, an IoT application architecture in which middleware plays a critical role in ensuring seamless data flow between physical devices, applications, and digital twins is represented. In IoT systems integrated with DTs, middleware not only synchronizes real data with virtual models, but also ensures that these digital replicas remain accurate and up-to-date. This allows DTs to simulate, analyze, and predict behaviors in systems such as home energy management, where real-time energy consumption data from sensors can serve as the basis for optimization and control decisions. By connecting various IoT devices and supporting edge processing, middleware improves both scalability and responsiveness, enabling digital twins to operate effectively in real-time and even offline, making them powerful tools for proactive management in complex environments such as Smart Homes and Smart Cities.