How to monitor connected and disconnect user IP Address in Nodejs

Nodejs have the ability to track the connected and disconnected user IP Address with socket.io node package.

What is Socket.io ?

Socket.io enables real-time directions which is event based communication. Its works on all the platform, browser or device on reliability and speed. We need to check few package it’s installed or not.  if not you can install socket.io and ip both package from your terminal.

Installing socket.io package

Installing socket.io package

Installing ip package

Installing ip package

Once you have installed both the packages, let’s include our needed package into our code and create server where our code will on that port.

var app = require('http').createServer(handler),
	io = require('socket.io').listen(app),
	ip = require('ip');
	app.listen(8001);

handler function will print “Welcome” in browser.

function handler(req,res) {
	res.writeHead(200);
	res.end('Welcome');
}

finally our socket which will monitor connected and disconnected users IP Address.

io.sockets.on('connection', function(socket) {
	console.log('User Connected from IP Address : '+ip.address());
	
	socket.on("disconnect", function(socket) {
		console.log('User DisConnected from the IP Address : '+ip.address());
	});
	
});

Run the above script in terminal.

executing nodejs program

executing nodejs program

Nodejs connected and disconnect user IP Address

Nodejs connected and disconnect user IP Address

Share