Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is socket.io emit callback appropriate?

Recently I have been messing around with socket.io and found this interesting thing, that I can have emit function callback like this.

I start emitting on client side like this:

client.emit('eventToEmit', dataToEmit, function(error, message){     console.log(error);     console.log(message); }); 

Then I can fire a callback from server-side like this:

client.on('eventToEmit', function(data, callback){     console.log(data);     callback('error', 'message'); }); 

Everything works fine with no errors, but I am interested if doing something like this is appropriate since I have not seen anything similar in the documentation or any example so far.

like image 742
antanas_sepikas Avatar asked Dec 02 '13 21:12

antanas_sepikas


People also ask

Is Socket.IO emit asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.

What does Socket.IO emit do?

emit() to send a message to all the connected clients. This code will notify when a user connects to the server. socket.

Which is better Socket.IO or WS?

Socket.IO is way more than just a layer above WebSockets, it has different semantics (marks messages with name), and does failovers to different protocols, as well has heartbeating mechanism. More to that attaches ID's to clients on server side, and more. So it is not just a wrapper, it is full-featured library.

Does Socket.IO use WebSockets?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.


1 Answers

It's perfectly legal.

Those callbacks are called 'acknowledgement functions' and are summarily mentioned in the Wiki and described a bit more in detail on the NPM page ('Getting acknowledgements').

EDIT: more recent documentation can be found here.

like image 62
robertklep Avatar answered Oct 01 '22 07:10

robertklep