Why doesn't my server respond to an emitted event by the client? I have tried a few trivial examples from the socket.io webpage and they seem to be working fine.
My goal is to emit an event whenever a user focuses out from the input box, compare the input value on the server, and fire an event back to the client.
client-side
$('#userEmail').focusout(function() {
var value = $('#userEmail').val(); // gets email from the input field
console.log(value); // prints to console (it works!)
socket.emit('emailFocusOut', { userEmail: value }); // server doesn't respond to this
});
server-side
io.sockets.on 'emailFocusOut', (data) ->
console.log(data)
Additional info
If you need some answer from server your server should emit message back to client.
console.log
does not do network answer.
var io = require('socket.io').listen(80);
io.sockets.on('connection', function(socket) {
socket.on('emailFocusOut', function(data) {
data.receivedAt = Date.now();
socket.emit('emailFocusOutResponse', data); // answer back
});
});
Then on client you can listen for 'emailFocusOutResponse'
and handle this message.
You have to put your custom event inside the io.sockets.on
function. The following code will work:
io.sockets.on('connection', function (socket) {
socket.on("emailFocusOut", function(data) {
console.log(data) // results in: { userEmail: 'awesome' }
})
});