Receive Acknowledgements in the Browser

On the client side, pass a callback as the final argument to socket.emit().

The callback receives the object returned by the server.

Update the message submission logic to display the acknowledgement result.

Client Acknowledgement
socket.emit('chat:send', {
  username,
  message
}, (response) => {
  if (!response.success) {
    console.error(response.error);
    return;
  }

  console.log(response.message);
});

The callback receives the acknowledgement returned by the server and allows the frontend to handle successful and failed submissions.

Key Takeaways
  • The client can pass an acknowledgement callback to socket.emit().

  • The server controls when the acknowledgement is returned.

  • Acknowledgements help implement responsive user interfaces.