0
  1. I am using Node.js for emitting event to socket via socket.emit(eventName[, ...args][, ack]).

  2. On client side, an Arduino, I'm using the SocketIoClient.h library (link: https://github.com/timum-viw/socket.io-client).

So the server is sending an event and a callback function as

socket.emit("event",function(x){
   console.log(x);
})

which is received on the client side in payload field as

clientSocket.on("event",event);
void event(const char * payload, size_t length);

How can I call this callback function from the client side, which is supposed to be present in the payload?

1 Answers1

0

If I understood you correctly, you are trying to use the acknowledgement mechanism offered by Socket.IO (I inferred this from the tags of your question).

For more details, refer to

This mechanism is specific to the Socket.IO library and therefore can't be used if you are not using it on both the client and the server sides.

As an alternative, I would suggest to dedicate a specific event to the acknowledgement you wish to have. In other words, when your client receives your event "event", it will send a "received" event to the server as acknowledgement along with the data your callback should be called with.

For instance on your server you would have:

socket.on("received", function (x) {
    console.log(x); //Will display "Yay! Got it!"
});
socket.emit("event");

and on the client something like

socket.on("event", function () {
    socket.emit("received", "Yay! Got it!");
});

(I'm not familiar with the C library you use but the point is the underlying logic).

Bert.e
  • 107
  • 2
  • 13
  • yeah, I had a thought of this.But, i have already written my server codes.Now i have to change them. I'm gonna wait for some more answers. thank you @Bret.e – Sarthak_ssg5 Aug 22 '17 at 15:18