This is definitely a very naive question since I am new to node.js.
I am just running the very first simple socket.io example from the website: http://socket.io/#how-to-use
client: index.html:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
server: app.js:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
From my understanding I assume what will happen is that when server receives a request from the client it will output the data emitted by client {my: 'data'}
to the console, which it does, but then it should also send to client the data {hello: 'world'}
and when client receives, it is also supposed to log the data to the console? On the terminal console I only see {my: 'data'}
for every request. Is the server not sending back data to client correctly or I am looking at the wrong place?
Apparently I think I got confused on some very basic stuff, but when telling the client to do console.log in client side javascript, is the output supposed to be shown on the terminal that executes the server program or something else? If I need to check the output on client console, where should I look for, in the browser?
Hope someone could tell me about this basic concept, I googled a lot but I believe it is just too simple that most just assume people know it so I did not get much.
On Chrome anywhere in the browser window -> rightclick for menu -> inspect element. This will pop up a window at the bottom of the main window. From the list of tabs 'Elements', 'Resources', 'Network'... navigate to 'Console'. All console logs appear here.