0

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.

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

Client:

<?php
    $url = $_SERVER["REQUEST_URI"];
?>

<body style="overflow: hidden;" />

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="/node_modules/socket.io/lib/socket.io.js"></script>

<script>

    var socket = io.connect('http://localhost:5000');

    socket.on('connected', function (data) {
        $("#log").append('Connected!<br />');
        $("#log").append(data + "<br />");
        socket.emit('viewing', { page: '<?php echo $url; ?>' });
            socket.on('disconnected', function(data) {
            $("#log").append("User Disconnected" + "<br />");
        });
    });

</script>

<div id='log' style='height: 100%; border: 1px solid black;'></div>

Server:

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 + "!" });
    });
});

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

any hints/ideas on what I might be doing wrong.

thank-you in advance -- Vinny

2 Answers 2

2

Also it seem that you are including server side socket.io code. Correct script tag would be this:

<script src="http://localhost:5000/socket.io/socket.io.js"></script> 
Sign up to request clarification or add additional context in comments.

3 Comments

wow I was going to say it shouldnt matter as the socket.io.js is in the same relative direcotry for both client page and server script, but this actually worked, thankyou very much, only 1 problem, how do I get rid of the socket.io debug messages, they're annoying me
To hide debug messages, add this under "var io = ..." line: io.set('log level', 1); Possible log levels are explained here: github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
thanks, I actually figured it out ".listen(5000, { log: false })"
1

In the line you write, there are some problems in your code.

socket.on('viewing', function(data){
    console.log("User Is Viewing: " + data);//it should be data.page
});

Also, when the disconnect event happened at the server side, the user has already left the page and the socket is closed so you can't send anything to the client. A solution for this is to send the disconnected event from the client side. There are events that indicates a user leaves a page such as the onbeforeunload event.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.