Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket.io send/emit with Callback #17

Open
BlackHornet opened this issue May 14, 2014 · 1 comment
Open

Socket.io send/emit with Callback #17

BlackHornet opened this issue May 14, 2014 · 1 comment

Comments

@BlackHornet
Copy link

Hi,

first of all: Wonderful Socket.IO framework :)

But I still have a small request, I wonder how to realize the best, maybe it's on your agenda, or you have other ideas how to solve this (if technically not possible):

referring http://socket.io/#how-to-use there is something called callbacks (definable within a send or emit):

Sending and getting data (acknowledgements). 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:
SERVER
var io = require('socket.io').listen(80);

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

CLIENT

  var socket = io.connect(); // TIP: .connect with no args does auto-discovery
  socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!
    socket.emit('ferret', 'tobi', function (data) {
      console.log(data); // data will be 'woot'
    });
  });

Is this somehow possible?

Kind regards
BlackHornet

@BlackHornet BlackHornet changed the title Socket.io emit with Callback Socket.io send/emit with Callback May 14, 2014
@snehasis
Copy link

snehasis commented Jul 8, 2014

Is there anyone have answer for this ? the Event call back is not working when using it like below.
SIOClient *sio = SIOClient::connect("http://192.168.1.231:3001");
TestTarget *target = new TestTarget();

sio->on("connect", target, callback(&TestTarget::ontestevent));
sio->on("testevent", target, callback(&TestTarget::ontestevent));
sio->emit("testevent","[{"username":"demo","password":"demo"}]");

Please how can I receive callback response from the server ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants