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
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' }
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With