DEV Community

Mahmudur Rahman
Mahmudur Rahman

Posted on

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 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
Enter fullscreen mode Exit fullscreen mode

Or using yarn:

yarn add conntrack
Enter fullscreen mode Exit fullscreen mode

๐Ÿ› ๏ธ 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");
});
Enter fullscreen mode Exit fullscreen mode

๐ŸŽญ 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"));
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

โš›๏ธ 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;
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”” 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);
});
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ deviceConnected

Triggered when a new device joins.

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

3๏ธโƒฃ deviceDisconnected

Triggered when a device leaves.

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

๐Ÿš€ Try It Out!

conntrack - npm

Real-time connected device tracking with Socket.IO. Latest version: 1.0.0, last published: 14 days 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. ๐Ÿ’ก

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay