<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Himanshu Mishra</title>
    <description>The latest articles on DEV Community by Himanshu Mishra (@himanshumishir).</description>
    <link>https://dev.to/himanshumishir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F728204%2F1e2fad03-a9e5-42d8-ad8a-41286986de98.JPG</url>
      <title>DEV Community: Himanshu Mishra</title>
      <link>https://dev.to/himanshumishir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/himanshumishir"/>
    <language>en</language>
    <item>
      <title>Building a Scalable Middleware for Face Recognition Attendance Devices Using Node.js and MQTT</title>
      <dc:creator>Himanshu Mishra</dc:creator>
      <pubDate>Sat, 03 May 2025 16:14:27 +0000</pubDate>
      <link>https://dev.to/himanshumishir/building-a-scalable-middleware-for-face-recognition-attendance-devices-using-nodejs-and-mqtt-4la4</link>
      <guid>https://dev.to/himanshumishir/building-a-scalable-middleware-for-face-recognition-attendance-devices-using-nodejs-and-mqtt-4la4</guid>
      <description>&lt;p&gt;Face recognition attendance systems are becoming more common in large enterprises. But integrating with them—especially when you're dealing with thousands of employees and dozens of devices—is far from straightforward. In this post, I’ll walk through how I built a scalable middleware using Node.js and MQTT to interface with smart attendance devices, handling real-time syncing, deletions, and multi-device management.&lt;/p&gt;

&lt;p&gt;🧩 The Problem&lt;br&gt;
We were working with face recognition devices that communicate via MQTT. These devices have their own set of protocols for:&lt;/p&gt;

&lt;p&gt;Real-time recognition data uploads&lt;/p&gt;

&lt;p&gt;Batch user syncing&lt;/p&gt;

&lt;p&gt;User deletion&lt;/p&gt;

&lt;p&gt;Intruder detection&lt;/p&gt;

&lt;p&gt;Device metadata management&lt;/p&gt;

&lt;p&gt;Our goal was to:&lt;/p&gt;

&lt;p&gt;Sync thousands of employee records across devices.&lt;/p&gt;

&lt;p&gt;Handle new employee additions, deletions, and updates efficiently.&lt;/p&gt;

&lt;p&gt;Receive and process real-time attendance events.&lt;/p&gt;

&lt;p&gt;Scale with increasing devices and users.&lt;/p&gt;

&lt;p&gt;🔧 The Tech Stack&lt;br&gt;
Node.js for building the middleware service.&lt;/p&gt;

&lt;p&gt;MQTT.js to manage communication with devices.&lt;/p&gt;

&lt;p&gt;MySQL for persistent storage of employee and device metadata.&lt;/p&gt;

&lt;p&gt;Redis for job queues and temporary state tracking.&lt;/p&gt;

&lt;p&gt;Docker + CI/CD for smooth deployments.&lt;/p&gt;

&lt;p&gt;🏗 Architecture Overview&lt;br&gt;
plaintext&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
Employee DB/API ↔ Middleware ↔ MQTT Broker ↔ Devices&lt;br&gt;
                            ↕&lt;br&gt;
                    MySQL, Redis&lt;br&gt;
Key Components:&lt;br&gt;
mqtt.js: Handles connection, reconnection logic, message parsing, and publishing.&lt;/p&gt;

&lt;p&gt;syncManager.js: Manages batch sync of employee data to devices.&lt;/p&gt;

&lt;p&gt;deletionManager.js: Handles scheduled and manual deletion jobs.&lt;/p&gt;

&lt;p&gt;eventHandler.js: Parses and logs attendance and intruder data sent from devices.&lt;/p&gt;

&lt;p&gt;🧠 Challenges Faced&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Device Acknowledgments
Each device expects an acknowledgment after every message. Missing an ACK could cause it to retry or drop the operation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Solution: Created a robust messageDispatcher that tracks correlation IDs and ensures timely ACKs with retry logic.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bulk Employee Syncing
Some devices had memory limits, so syncing 10k users at once caused crashes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Solution: Introduced batching and throttling using a queue-based worker system:&lt;/p&gt;

&lt;p&gt;js&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
const batchSize = 100;&lt;br&gt;
const delay = 200; // ms&lt;br&gt;
Each batch was sent with a delay, and progress was tracked via Redis.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;MQTT Reconnection Logic
MQTT connections sometimes dropped silently.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Solution: Added:&lt;/p&gt;

&lt;p&gt;Heartbeat topics&lt;/p&gt;

&lt;p&gt;Reconnection backoff&lt;/p&gt;

&lt;p&gt;On-demand manual reconnect trigger via API&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Multi-device Management
Some employees needed to be synced only to specific devices.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Solution: Maintained a many-to-many relationship between employees and devices in MySQL, and dynamically selected the devices for each operation.&lt;/p&gt;

&lt;p&gt;🛠 Sample Code Snippet&lt;br&gt;
js&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
mqttClient.on('message', async (topic, message) =&amp;gt; {&lt;br&gt;
  const parsed = JSON.parse(message.toString());&lt;/p&gt;

&lt;p&gt;if (topic.includes('attendance')) {&lt;br&gt;
    await handleAttendanceLog(parsed);&lt;br&gt;
  } else if (topic.includes('ack')) {&lt;br&gt;
    ackTracker.resolve(parsed.requestId, parsed.status);&lt;br&gt;
  }&lt;br&gt;
});&lt;br&gt;
📈 Performance&lt;br&gt;
After optimizing:&lt;/p&gt;

&lt;p&gt;50+ devices could be managed concurrently.&lt;/p&gt;

&lt;p&gt;20,000+ employee records synced without timeouts or failures.&lt;/p&gt;

&lt;p&gt;Real-time event processing latency: &amp;lt; 500ms&lt;/p&gt;

&lt;p&gt;🧪 Testing Tips&lt;br&gt;
Use an MQTT simulator to mimic device behavior.&lt;/p&gt;

&lt;p&gt;Validate ACK timeouts and retry logic with offline devices.&lt;/p&gt;

&lt;p&gt;Monitor logs with correlation IDs for each operation.&lt;/p&gt;

&lt;p&gt;🚀 Final Thoughts&lt;br&gt;
This project taught me a lot about building real-time, fault-tolerant systems in Node.js. MQTT is powerful but tricky—it requires strict protocol adherence, retry handling, and thoughtful scaling strategies.&lt;/p&gt;

&lt;p&gt;Have you worked with IoT devices or MQTT in production? I’d love to hear your experiences!&lt;/p&gt;

&lt;p&gt;💬 Let’s Connect:&lt;br&gt;
You can find me on GitHub [&lt;a class="mentioned-user" href="https://dev.to/himanshumishir"&gt;@himanshumishir&lt;/a&gt;] or check out my portfolio [&lt;a href="https://himanshumishir.in" rel="noopener noreferrer"&gt;https://himanshumishir.in&lt;/a&gt;].&lt;/p&gt;

</description>
      <category>mqtt</category>
      <category>node</category>
      <category>middleware</category>
      <category>himanshumishir</category>
    </item>
    <item>
      <title>How to send mail using Nodemailer?</title>
      <dc:creator>Himanshu Mishra</dc:creator>
      <pubDate>Sun, 20 Feb 2022 18:34:30 +0000</pubDate>
      <link>https://dev.to/himanshumishir/how-to-send-mail-using-nodemailer-ol5</link>
      <guid>https://dev.to/himanshumishir/how-to-send-mail-using-nodemailer-ol5</guid>
      <description>&lt;h2&gt;
  
  
  What is nodemailer?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Nodemailer&lt;/strong&gt; is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why nodemailer?
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Nodemailer features&lt;/em&gt;&lt;br&gt;
A single module with zero dependencies – code is easily auditable, as there are no dark corners&lt;br&gt;
Heavy focus on security, no-one likes RCE vulnerabilities&lt;br&gt;
Unicode support to use any characters, including emoji 💪&lt;br&gt;
Windows support – you can install it with npm on Windows just like any other module, there are no compiled dependencies. Use it hassle free from Azure or from your Windows box&lt;br&gt;
Use HTML content, as well as plain text alternative&lt;br&gt;
Add Attachments to messages&lt;br&gt;
Embedded image attachments for HTML content – your design does not get blocked&lt;br&gt;
Secure email delivery using TLS/STARTTLS&lt;br&gt;
Different transport methods in addition to the built-in SMTP support&lt;br&gt;
Sign messages with DKIM&lt;br&gt;
Custom Plugin support for manipulating messages&lt;br&gt;
Sane OAuth2 authentication&lt;br&gt;
Proxies for SMTP connections&lt;br&gt;
ES6 code – no more unintentional memory leaks, due to hoisted var’s&lt;br&gt;
Autogenerated email test accounts from Ethereal.email`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step by Step guide on how to send mail&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open terminal.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
mkdir node-mail
cd node-mail
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create server.js file.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
touch server.js
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a node app.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
npm init -y
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install express and nodemailer.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
npm install nodemailer express
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create transportOptions.js and message.js.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
touch message.js transportOptions.js
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Open message.js and export an object.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
module.exports = (email)=&amp;gt;{
return {
  from: "test@mail.com",
  to: email,
  subject:"Sample mail ",
  text: "Hello",
  html: "&amp;lt;h1 style="color:#183b56;font-size: 28px;text-align: center;"&amp;gt;Hello User&amp;lt;/h1&amp;gt;
    &amp;lt;p style="font-size: 18px;color: #1f2933;text-align: center;"&amp;gt;Hello this is a test mail&amp;lt;/p&amp;gt;",
}
};
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Open transportOptions.js and export an object here also.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
module.exports =  transportOptions = {
host: "smtp.office365.com",
port: "587",
auth: { user: "test@mail.com", pass: "PASSWORD" },
secureConnection: true,
tls: { ciphers: "SSLv3" },
};
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Open server.js and create an express server.
&lt;code&gt;&lt;/code&gt;&lt;code&gt;
const express = require('express');
const transportOptions = require('./transportOptions');
const message = require('./message');
const app = express();
app.get('/send-mail', (req, res) =&amp;gt; {
const {email} = req.body;
(async () =&amp;gt; {
try {
  const mailTransport = 
nodemailer.createTransport(transportOptions);
  await mailTransport.sendMail(message(email));
  return res.status(200).json({
    message: "Successfully sent mail!",
  });
} catch (err) {
  return res.status(400).json({
    message: "Sorry No such Email Exist",
  });
}
})();
});
app.listen(3000, () =&amp;gt; console.log('Example app is listening on port 3000.'));
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Save all files and test.&lt;/li&gt;
&lt;li&gt;Please comment for any suggestion or feedback.&lt;/li&gt;
&lt;li&gt;You can contact me on &lt;a href="mailto:HimanshuMishra@duck.com"&gt;HimanshuMishra@duck.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>nodemailer</category>
      <category>node</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
