Skip to content

Commit

Permalink
Merge pull request #45 from Garrett04/main
Browse files Browse the repository at this point in the history
Add socket.io client connection and handle real-time stats for React
  • Loading branch information
KorryKatti authored Oct 16, 2024
2 parents 2349f1f + 0f15ddf commit 82a66d0
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 6 deletions.
101 changes: 100 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
"dependencies": {
"@vitejs/plugin-react": "^4.3.2",
"react": "^18.3.1",
"react-countup": "^6.5.3",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.27.0"
"react-router-dom": "^6.27.0",
"socket.io-client": "^4.8.0"
}
}
32 changes: 29 additions & 3 deletions src/components/Body.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import React from "react";
import React, { useEffect, useState } from "react";
import Footer from "./Footer";
import { TfiLock } from "react-icons/tfi";
import { FiEyeOff } from "react-icons/fi";
import { FiShield } from "react-icons/fi";
import { io } from 'socket.io-client';
import CountUp from 'react-countup';

const Body = () => {
const [dailyUsersCount, setDailyUsersCount] = useState(0);
const [todaysUserCount, setTodaysUsersCount] = useState(0);

useEffect(() => {
const socket = io('https://mirage-o081.onrender.com/');

socket.on('connect', () => {
// console.log('Successfully connected to Mirage test net server!', socket.id);

// Get metrics once connected to server
socket.emit('get_metrics');

// Update daily users count and todays users count states
socket.on('metrics', (data) => {
// console.log(data);
setDailyUsersCount(data.total);
setTodaysUsersCount(data.today);
})
})

// Close socket connection
return () => socket.disconnect();
}, []);

return (
<div className="bg-gradient-to-b from-[#4e5279] via-[#ebc5e4] to-[#cf8ba9]">
<div className="flex justify-between -mb-4">
Expand Down Expand Up @@ -36,8 +62,8 @@ const Body = () => {

<div className="mt-24 mr-10 py-5 px-8 mb-32 bg-[#2A2D47] rounded-xl shadow-md text-center font-semibold text-white w-1/3">
<div className="">
<h2>Daily Users: 0</h2>
<h2>Today's Users: 0</h2>
<h2>Daily Users: <CountUp start={0} end={dailyUsersCount}></CountUp></h2>
<h2>Today's Users: <CountUp start={0} end={todaysUserCount}></CountUp></h2>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import ReactDOM from "react-dom";
import ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import App from "./App";
import "./index.css";
Expand Down

0 comments on commit 82a66d0

Please sign in to comment.