Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Acknowlegement vs Emit in Socket IO

When should I use acknowledgment over emit in socket.io? Should it be used for data transport as a request/response.

Let's say I have an app with an endpoint that asks for a list of conversations, the server will contain a listener called my-conversations. To send data back to the caller, should I use acknowledgment callback or should I emit it?

like image 721
Maksim Avatar asked Aug 22 '16 18:08

Maksim


People also ask

What is difference between Socket on and Socket emit?

socket. emit - This method is responsible for sending messages. socket. on - This method is responsible for listening for incoming messages.

What is the use of Socket emit?

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

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.


1 Answers

Imho, "emit" was created to send datas, but "acknowledgment" exists to tell you that some datas were correctly received by your pair.

This is especially usefull when you're checking data integrity with calculation or cryptography.

It is mostly used to avoid repetitive pieces of code, and saves the pain of having classical paired call/response events...

So, Question 1 : use it when you want to confirm that datas were correctly received.
Question 2 : you can use it automatically as a data-transport checking "personnal protocol".
Question 3 : in that specific case, prefer "emit" to send datas, but the caller can send an acknowledgment to the emitter after receiving datas (or not, depending of datas integrity).

More informations :
Acknowledgement_(data_networks)

like image 144
Aethyn Avatar answered Sep 20 '22 18:09

Aethyn