Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io not firing events from client to server

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

  • express 3.0rc4
  • socket.io 0.9.10
  • coffee-script 1.3.3
like image 368
Sahat Yalkabov Avatar asked Dec 21 '22 15:12

Sahat Yalkabov


1 Answers

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' }
  })
});
like image 170
zemirco Avatar answered Dec 24 '22 01:12

zemirco