Ata
Ata

Reputation: 12544

Check if the user received message with socket io

I have simple question , think I sent a message with socket.emit in server to client , so how can I check if the user really got the message , does socket io or node have any built in system

, I know that I can use ping user with socket.emit and after that user send back a response that got server message , but I need to know does socket io have such bulit in system ?

thanks.

Upvotes: 2

Views: 3612

Answers (1)

go-oleg
go-oleg

Reputation: 19480

Yes, you can do that by passing a function as the last parameter to .emit.

Example from the "Sending and getting data (acknowledgements)" section of socket.io:

server:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.on('ferret', function (name, fn) {
    fn('woot');
  });
});

client:

<script>
  var socket = io.connect();
  socket.on('connect', function () {
    socket.emit('ferret', 'tobi', function (data) {
      console.log(data); // data will be 'woot'
    });
  });
</script>

Upvotes: 2

Related Questions