ConnTrack: Real-time Device Connection Tracker for Your Web Apps

Hey Devs! ๐Ÿ‘‹

If you’ve ever wanted to track the number of connected users on your app in real-time, you’re in luck! I’ve built ConnTrack, a lightweight npm package that helps you monitor connected devices using Socket.IO effortlessly. Whether you’re wo…


This content originally appeared on DEV Community and was authored by Mahmudur Rahman

Hey Devs! ๐Ÿ‘‹

If you've ever wanted to track the number of connected users on your app in real-time, you're in luck! I've built ConnTrack, a lightweight npm package that helps you monitor connected devices using Socket.IO effortlessly. Whether you're working with Node.js, React, EJS, or vanilla HTML, ConnTrack provides instant updates on device connections and disconnections.

๐ŸŒŸ Why Use ConnTrack?

  • ๐Ÿ“ก Real-time device tracking
  • ๐Ÿ”„ Automatic connection/disconnection updates
  • ๐Ÿ”ฅ Plug-and-play with Express, React, and more
  • ๐Ÿ›  Simple API with minimal setup

๐Ÿ“ฆ Installation

Install ConnTrack via npm:

npm install conntrack

Or using yarn:

yarn add conntrack

๐Ÿ› ๏ธ Usage

๐Ÿš€ Node.js (Express Server) Implementation

const express = require("express");
const http = require("http");
const { initDeviceTracker } = require("conntrack");

const app = express();
const server = http.createServer(app);
const { io } = initDeviceTracker(server);

io.on("deviceConnected", (data) => {
    console.log(`๐Ÿ”ต Device Connected: ${data.id}`);
});

io.on("deviceDisconnected", (data) => {
    console.log(`๐Ÿ”ด Device Disconnected: ${data.id}`);
});

server.listen(3003, () => {
    console.log("๐Ÿš€ Server running on http://localhost:3003");
});

๐ŸŽญ EJS Integration (Server-side Rendering)

Server Setup

const express = require("express");
const http = require("http");
const { initDeviceTracker } = require("conntrack");

const app = express();
const server = http.createServer(app);
const { io } = initDeviceTracker(server);

app.set("view engine", "ejs");
app.get("/", (req, res) => {
    res.render("index");
});

server.listen(3004, () => console.log("๐Ÿš€ Server running on http://localhost:3004"));

EJS View File (views/index.ejs)

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
    <script>
        const socket = io();
        socket.on("deviceUpdate", (data) => {
            document.getElementById("count").innerText = data.count;
        });
    </script>
</head>
<body>
    <h1>Connected Devices: <span id="count">0</span></h1>
</body>
</html>

โš›๏ธ React.js Implementation

import { useEffect, useState } from "react";
import { io } from "socket.io-client";

const socket = io("http://localhost:3003");

function DeviceTracker() {
    const [deviceCount, setDeviceCount] = useState(0);

    useEffect(() => {
        socket.on("deviceUpdate", (data) => {
            setDeviceCount(data.count);
        });
    }, []);

    return <h1>Connected Devices: {deviceCount}</h1>;
}

export default DeviceTracker;

๐Ÿ”” Real-Life Use Cases

๐Ÿš€ Live Streaming Apps โ€“ Track the number of viewers in real time.

๐Ÿ“Š Admin Dashboards โ€“ Monitor active users on your platform.

๐ŸŽฎ Multiplayer Games โ€“ Keep count of connected players.

๐Ÿ“ก IoT Applications โ€“ Track real-time connections of IoT devices.

๐Ÿ“œ API Events

1๏ธโƒฃ deviceUpdate

Emitted when a device connects or disconnects.

socket.on("deviceUpdate", (data) => {
    console.log("Connected Devices:", data.count);
});

2๏ธโƒฃ deviceConnected

Triggered when a new device joins.

socket.on("deviceConnected", (data) => {
    console.log("New Device Connected:", data.id);
});

3๏ธโƒฃ deviceDisconnected

Triggered when a device leaves.

socket.on("deviceDisconnected", (data) => {
    console.log("Device Disconnected:", data.id);
});

๐Ÿš€ Try It Out!

conntrack - npm

Real-time connected device tracking with Socket.IO. Latest version: 1.0.0, last published: 7 minutes ago. Start using conntrack in your project by running `npm i conntrack`. There are no other projects in the npm registry using conntrack.

favicon npmjs.com



conntrack makes it easy to implement real-time device tracking. Give it a shot and let me know your thoughts in the comments! ๐Ÿ˜Š

Happy coding! โœจ

I would love to hear your feedback! ๐Ÿš€ Let me know if you have any feature requests. ๐Ÿ’ก


This content originally appeared on DEV Community and was authored by Mahmudur Rahman


Print Share Comment Cite Upload Translate Updates
APA

Mahmudur Rahman | Sciencx (2025-02-16T01:48:33+00:00) ConnTrack: Real-time Device Connection Tracker for Your Web Apps. Retrieved from https://www.scien.cx/2025/02/16/conntrack-real-time-device-connection-tracker-for-your-web-apps/

MLA
" » ConnTrack: Real-time Device Connection Tracker for Your Web Apps." Mahmudur Rahman | Sciencx - Sunday February 16, 2025, https://www.scien.cx/2025/02/16/conntrack-real-time-device-connection-tracker-for-your-web-apps/
HARVARD
Mahmudur Rahman | Sciencx Sunday February 16, 2025 » ConnTrack: Real-time Device Connection Tracker for Your Web Apps., viewed ,<https://www.scien.cx/2025/02/16/conntrack-real-time-device-connection-tracker-for-your-web-apps/>
VANCOUVER
Mahmudur Rahman | Sciencx - » ConnTrack: Real-time Device Connection Tracker for Your Web Apps. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/16/conntrack-real-time-device-connection-tracker-for-your-web-apps/
CHICAGO
" » ConnTrack: Real-time Device Connection Tracker for Your Web Apps." Mahmudur Rahman | Sciencx - Accessed . https://www.scien.cx/2025/02/16/conntrack-real-time-device-connection-tracker-for-your-web-apps/
IEEE
" » ConnTrack: Real-time Device Connection Tracker for Your Web Apps." Mahmudur Rahman | Sciencx [Online]. Available: https://www.scien.cx/2025/02/16/conntrack-real-time-device-connection-tracker-for-your-web-apps/. [Accessed: ]
rf:citation
» ConnTrack: Real-time Device Connection Tracker for Your Web Apps | Mahmudur Rahman | Sciencx | https://www.scien.cx/2025/02/16/conntrack-real-time-device-connection-tracker-for-your-web-apps/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.