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...