This requires client to server communication, which easily can be done using Socket IO
Socket IO is bidirectional and event-based communication between the browser and the server.
You can install it using npm,
npm i --save socket.io
or just have it globally,
npm i socket.io
In back end (node js), we initialize the socket by passing the server object (http) then listen for incoming sockets
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('click', (data) => {
console.log("An element clicked")
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
in your case, you want to inform the server each time you clicks in the html
see the line, io.on('click', (data) => {
the 'click' is the click event name, it can be anything, like 'onclick' or 'mouse click' or 'mouse'
With that configured,
in front end (the html), load the socket io client and initialize it then emit the click event like below
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
window.onload = () => {
const socket = io();
$('#btn').click(() => {
e.preventDefault(); // prevents page reloading
socket.emit('click', 'hello server!'));
return false;
});
});
</script>
as you can see, im using
socket.emit(event, data);
this line will emit 'click' event which the server is listening to.
Hope this helps ;)
Visit https://socket.io for more information
app.jsis server code. In order to appear in the server, you'll have to listen to the click event in the client and in that event handler, send some request to the server. In the server you'll have to react to it somehow (the easiest is registering a URL for that event withapp.get()and there put the log code)