This example from Socket.IO website is confusing me. Sending and getting data (acknowledgements):
Client:
<script>
socket.on('connect', function () {
socket.emit('ferret', 'tobi', function (data) {
console.log(data); // data will be 'woot'
});
});
</script>
Server:
io.sockets.on('connection', function (socket) {
socket.on('ferret', function (name, fn) {
fn('woot');
});
});
I'm actually reproducing this example. What I can't understand is:
- Q1: How does this work in the first place. Does the server (when executing
fn
) automagically emits the result to the client? Does Socket.IO bindfn
to the client third parameter ofemit
? - Q2: What's the (unused)
name
parameter in server anonymousfunction (name, fn)
? Logging it shows that it'sundefined
, why?