0

I have 2 parts of my app the register, and the index/login, the case is that i want to update every time an user gets registered, that's done the difficult part of this is make that the index shows a query from the begining and not being send by the register. Code:

Index script:

<script>
    var socket = io();
    socket.on('registered', function (msg) {
        $('#messages').empty().append(msg).hide().fadeIn(500);
        console.log(msg);
    });
    function message() {
        var mes = document.getElementById('text').value;
        console.log(mes);
        socket.emit('registered', mes);
    }
</script>

Index code to get users:

global.mysql.query("SELECT * FROM users ORDER BY id DESC LIMIT 4", function (err, rows, fields) {
        if (err) throw err;
        var msg = "";
        for(var i = 0; i <= rows.length-1; i++) {
            msg += rows[i].username + '<br>';
        }

        global.io.sockets.emit('registered', msg);

    })

Code from register:

router.post('/register', function(req, res, next) {
    global.mysql.query("INSERT INTO users (id, username) VALUES (?,?)", [null, req.body.user], function () {
        global.mysql.query("SELECT * FROM users ORDER BY id DESC LIMIT 4", function(err, rows, fields) {
            if (err) throw err;
            var msg = "";
            for(var i = 0; i <= rows.length-1; i++) {
                msg += rows[i].username + '<br>';
            }
            global.io.sockets.emit('registered', msg);
        })
    });
});

I don't know why because it's the same code...

  • Unrelated but you really shouldn't be using global variables like that. Instead of making it global just export the mysql and io objects and do like require('./mysql.js') and require('./io.js') – Christopher Tarquini Jul 18 '15 at 22:06
  • I know but is only a test project, i come from PHP and i see that it changes a lot, the only thing that i want is that work. Later i'll see how can i get better code :D –  Jul 18 '15 at 22:08
  • Just making sure you knew :). Have you tried opening up chrome/safari/firefox dev tools and checking to see if the web socket is open and sending any data? And can we see the code where you setup/attach socket.io to your http server? – Christopher Tarquini Jul 18 '15 at 22:10
  • How i can do this? Please tell me. –  Jul 18 '15 at 22:14
  • http://stackoverflow.com/questions/5751495/debugging-websocket-in-google-chrome – Christopher Tarquini Jul 18 '15 at 22:15
  • Also run your app with `DEBUG=* node myapp.js`. It'll give you more debugging info in your console to help you solve the problem (post the results here). See http://socket.io/docs/logging-and-debugging/ – Christopher Tarquini Jul 18 '15 at 22:16
  • I get this: ÿ0{"sid":"UELCWTkQ27-oLS1iAAAC","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000} With two previous bad requests... –  Jul 18 '15 at 22:19
  • http://pastebin.com/5TBB7wgj This is debugging –  Jul 18 '15 at 22:22
  • Did you create a user during this? I don't see anywhere that says the socket sent any data – Christopher Tarquini Jul 18 '15 at 22:24
  • The index js has to do that no? –  Jul 18 '15 at 22:33
  • this is the line when i send data: global.io.sockets.emit('registered', msg); –  Jul 18 '15 at 22:39
  • Did you actually create a user though (submit a POST request / form) while running with the debug log enabled? – Christopher Tarquini Jul 18 '15 at 22:43
  • No... I don't know im so new in Node.js i'm have 1 day in node... I so confused, forgive me but i'm so bad at fixing –  Jul 18 '15 at 22:52
  • `router.post('/register')` listens for a POST request. In order for it to be triggered you need create that request (generally with a HTML form or a XHR). For example `
    `
    – Christopher Tarquini Jul 18 '15 at 22:55
  • Damn it i fixed it XD The solution was put the io.on('connection') into the query callback... So stupid from my part. Sorry for spending your time D: –  Jul 18 '15 at 22:57
  • No worries, make sure you add that as an answer and accept it so anyone else who has this problem can find it (plus you get a badge for answering your own question!) – Christopher Tarquini Jul 18 '15 at 22:58

1 Answers1

0

I fixed it putting the io.on into the router:

router.get('/', function(req, res, next) {
    res.sendFile(global.path + '/views/index.html');
    global.mysql.query("SELECT * FROM users ORDER BY id DESC LIMIT 4", function (err, rows, fields) {
        if (err) throw err;
        var msg = "";
        for(var i = 0; i <= rows.length-1; i++) {
            msg += rows[i].username + '<br>';
        }
        global.io.on('connection', function(socket){
            console.log('a user connected');

            global.io.sockets.emit('registered', msg);

            socket.on('disconnect', function(){
                console.log('user disconnected');
            });
        });

    })
});

Hope it can help anyone!