Skip to content

Commit

Permalink
Add user count on endpoint online-users
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-ceccato committed Feb 6, 2024
1 parent 1164cc0 commit 966250e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const app = express();

const PORT = 35123;

// Object to store IP addresses and timestamps
const ipAddresses = {};

// Function to track IP addresses and timestamps
const trackIP = (ip) => {
ipAddresses[ip] = Date.now();
};


app.use(cors());
app.use(bodyParser.json());

Expand Down Expand Up @@ -111,13 +120,38 @@ getPomoStateJSON = () => {

// Define a route that serves the JSON
app.get("/json-endpoint", cors(), (req, res) => {
const ip = req.ip || req.connection.remoteAddress; // Get IP address
trackIP(ip); // Track IP
//console.log(
//"Received request from: " + req.ip + " at " + new Date().toISOString()
//);
//console.log(getPomoStateJSON());
res.json(getPomoStateJSON());
});

// Endpoint to display the number of online users
app.get('/online-users', (req, res) => {
// Call the function to count unique IPs in the last 5 seconds
const onlineUsersCount = countUniqueIPsInLast5Seconds();

// Respond to the request with the online users count
res.send(`Number of online users in the last 5 seconds: ${onlineUsersCount}`);
});

// Function to count unique IPs in the last 5 seconds
const countUniqueIPsInLast5Seconds = () => {
const currentTime = Date.now();
const threshold = currentTime - 5000; // 5 seconds ago

const uniqueIPs = Object.keys(ipAddresses).filter(
(ip) => ipAddresses[ip] >= threshold
);

console.log('Number of unique IPs in the last 5 seconds:', uniqueIPs.length);

return uniqueIPs.length;
};

// Start index.html server to avoid mixed content error
app.use(express.static(path.join(__dirname, '.')));
app.get('/', (req, res) => {
Expand Down

0 comments on commit 966250e

Please sign in to comment.