In the following code snippet taken from the Socket.IO website,
socket.set('nickname', name, function () {
socket.emit('ready');
});
when setting client data, what’s the passed function used for? Is it used simply to fire the function once the data is set? If so, what’s the difference with:
socket.set('nickname', name);
socket.emit('ready');
Also what is err used for in the following:
socket.get('nickname', function (err, name) {
console.log('Chat message by ', name);
});
That’s an async callback, which means it’s a function that will be called after the
setoperation was completedIt’s a big difference.
socket.setis non-blocking, which means the method will be called but the process will continue with the following code, without waiting for it to finish. This means thesocket.emitwill be called before thesetwas completed.err can be a connection error or anything that went wrong during the
getmethod