How to be sure that message via has been received to the client?

How to check that message sent with library has been received to the client.
Is there special method for it in

Thanks for your answers!

You should use the callback parameter while defining the event handler.

A typical implementation would be as follows:

Client side

var socket = io.connect('http://localhost');
socket.emit('set', 'is_it_ok', function (response) {

Server side

io.sockets.on('connection', function (socket) {
    socket.on('set', function (status, callback) {

Now check the console on the server side. It should display ‘is_it_ok’. Next check console on client side. It should display ‘ok’. That’s the confirmation message.


A connection is essentially persistent. The following in-built functions let you take action based on the state of the connection.

socket.on('disconnect', function() {} ); // wait for reconnect
socket.on('reconnect', function() {} ); // connection restored  
socket.on('reconnecting', function(nextRetry) {} ); //trying to reconnect
socket.on('reconnect_failed', function() { console.log("Reconnect failed"); });

Using the callback option shown above is effectively a combination of the following two steps:

socket.emit('callback', 'ok') // happens immediately

and on the client side

socket.on('callback', function(data) {

So you don’t need to use a timer. The callback runs immediately except if the connection has any of the following states – ‘disconnect’, ‘reconnecting’, ‘reconnect_failed’.

You can use the’s acknowledgements.

Quote from the documentation:

Sometimes, you might want to get a callback when the client confirmed
the message reception.

To do this, simply pass a function as the last parameter of .send or
.emit. What’s more, when you use .emit, the acknowledgement is
done by you, which means you can also pass data along.

On the client side simply emit the event with your data, the function will be called whenever the server responds to your event:

client.emit("someEvent", {property:value}, function (data) {
  if (data.error) 
    console.log('Something went wrong on the server');

  if (data.ok)
    console.log('Event was processed successfully');

On the server side you get called with the data and the callback handle to send the response:

socket.on('someEvent', function (data, callback) {

  // do some work with the data

  if (err) {
    callback({error:'someErrorCode', msg:'Some message'});


When you add a function as the last parameter of .send() or .emit() method calls, this function is called when the other party receives the message.

socket.send('hi', function() {
    // if we are here, our salutation has been received by the other party.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Count days until today moment.js

Similar Posts