Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Socket.io not firing events from client to server

Socket.io not firing events from client to server

Problem

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
Problem courtesy of: Sahat Yalkabov

Solution

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.

Solution courtesy of: Eugene Naydenov

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Socket.io not firing events from client to server

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×