Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js socket.io page viewers
    text
    copied!<p>I want to use node.js and socket.io to show how many users are currently viewing a certain page on my website, by sending a message with the page address (i.e /homepage or /events) to the node.js server, then read/write a file with the address name (i.e. homepage.txt, or events.txt), get the current count and add 1 then write it back to the file, then do the same on disconnect but instead remove 1 and save it.</p> <p>Here is the code I have so far, currently excluding the fs functions for testing, also the server file and client file are in the same directory</p> <p>Client: </p> <pre><code>&lt;?php $url = $_SERVER["REQUEST_URI"]; ?&gt; &lt;body style="overflow: hidden;" /&gt; &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="/node_modules/socket.io/lib/socket.io.js"&gt;&lt;/script&gt; &lt;script&gt; var socket = io.connect('http://localhost:5000'); socket.on('connected', function (data) { $("#log").append('Connected!&lt;br /&gt;'); $("#log").append(data + "&lt;br /&gt;"); socket.emit('viewing', { page: '&lt;?php echo $url; ?&gt;' }); socket.on('disconnected', function(data) { $("#log").append("User Disconnected" + "&lt;br /&gt;"); }); }); &lt;/script&gt; &lt;div id='log' style='height: 100%; border: 1px solid black;'&gt;&lt;/div&gt; </code></pre> <p>Server: </p> <pre><code>var io = require('socket.io').listen(5000); var connected_users = 0; io.sockets.on('connection', function(socket) { connected_users++; console.log("User Connected!"); socket.emit("connected", { message: "Users Online: " + connected_users + "!" }); socket.on('viewing', function(data){ console.log("User Is Viewing: " + data); }); socket.on('disconnect', function(){ connected_users--; console.log("User Disconnected!"); socket.emit("disconnected", { message: "Users Online: " + connected_users + "!" }); }); }); </code></pre> <p>but for some reason this doesn't seem to be working, all the server says is "info - socket.io started" and nothing else when a user connects to the client page</p> <p>any hints/ideas on what I might be doing wrong.</p> <p>thank-you in advance -- Vinny</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload