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?
socket. emit - This method is responsible for sending messages. socket. on - This method is responsible for listening for incoming messages.
emit() to send a message to all the connected clients. This code will notify when a user connects to the server. socket.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With