Nodejs Real time online users counter using socket io Example

Today, We want to share with you Nodejs Real time online users counter using socket io Example.In this post we will show you Showing online users using Nodejs and Socket.io, hear for Node JS – Get Real time online users counter using socket io example we will give you demo and example for implement.In this post, we will learn about Real time online activity monitor example with node.js and WebSocket with an example.

Nodejs Real time online users counter using socket io Example

There are the Following The simple About Nodejs Real time online users counter using socket io Example Full Information With Example and source code.

first of all You have latest verison installed nodejs, express as well as new socket.io package using npm, As I will cover this Post with live Working example to develop count online users on my website in real time with node.js, so the some major files and Directory structures for this example is following below Steps for real time chat with nodejs socket io and expressjs.

  • index.html
  • server.js

Nodejs Real time online users counter

index.html

This is where I will make a simple HTML form and Node JS server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.html.

<!DOCTYPE html>
<html>
<head>
   <title>Real time online activity monitor example with node.js and WebSocket</title>
   <script type="text/javascript" src="https://code.jquery.com/jquery-1.4.2.min.js"></script>
   <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.min.js"></script>
</head>

<body>

<h1>socket NodeJS Live Conter : <span id="socket_io_counter"></span></h1>
<b>socket io real time online users example</p>
<script type="text/javascript">
//socket io number of clients in room
  var socket = io.connect('http://localhost:8001');
  socket.on('socket_io_counter', function (response) {
    $("#socket_io_counter").text(response.flags);
  });
</script>
</body>
</html>

Step 2 : Node JS server.js

server.js

Simple Nodejs Real time online users counter for server.js files

var liveapp = require('express')();
var server = require('http').Server(liveapp);
//node js active users counter
var io = require('socket.io')(server);
//nodejs socket.io online users counter
server.listen(8001);
var flags = 0;
var $ipsConnected = [];
io.on('connection', function (socket) {
  var $liveIpAddress = socket.handshake.address;
  //check socket io online users
  if (!$ipsConnected.hasOwnProperty($liveIpAddress)) {
  	$ipsConnected[$liveIpAddress] = 1;
  	flags++;
	//socket io real time online users example
  	socket.emit('socket_io_counter', {flags:flags});
  }
  console.log("Good Luck, client is connected");
  /* Your Live (socket.io client flags) Disconnect socket */
  socket.on('disconnect', function() {
  	if ($ipsConnected.hasOwnProperty($liveIpAddress)) {
  		delete $ipsConnected[$liveIpAddress];
	    flags--;
	    socket.emit('socket_io_counter', {flags:flags});
  	}
  });
});

Save And run server.js file

run Project and Nodejs Get Real time online users counter using socket io Example

//Nodejs Real time online users counter
nodejs server.js

Nodejs Real time online users counter using socket io – Output

Nodejs Real time online users counter
Nodejs Real time online users counter
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Nodejs Real time online users counter using socket io Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   jQuery CDN Link Cheat Sheet Download
  2.   Upload Multiple Images With Image Preview Using jQuery,Ajax And PHP
  3.   Laravel Remove Public From URL Steps
  4.   Vuejs currency filter example
  5.   Top 10 MySQL Interview Questions and Answers
  6.   return view in new tab laravel Example Tutorial
  7.   jQuery Print Dynamic Content using PHP MySQL Example
  8.   Same Scope Variable in Multiple Controllers using Angular Example
  9.   Vue IF Else Conditional Statement
  10.   Generate Secure Random Strings using Laravel