Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Success Callback for emit method in socket.io

Im trying to emit a custom message from my client. I need to perform some actions on its success and failure. Now, how can i attach the success callback to emit method?

For error callback , i used Exposed events doc and got it working

socket.on('error', () -> console.log("Error Occured"))

For success, i tried

socket.emit('my custom method', {content: json},() -> console.log("Emitted"))

This callback is never been triggered irrespective whether its a success or failure.

How can i get hold of success handler?

like image 549
Prasanna Avatar asked Nov 25 '12 09:11

Prasanna


People also ask

Does socket emit have a callback?

According to the socket. emit() documentation, the acknowledgement functions (callbacks) must be the last parameters to the socket.

What is callback in socket?

on(event, callback) suggest edits. Set up callback functions for various events on the WebSocket connection. Multiple handlers can be defined for the same event.

Is Socket.IO emit asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.

What is socket on and socket emit?

socket. emit - This method is responsible for sending messages. socket. on - This method is responsible for listening for incoming messages.


2 Answers

If you look at the docs, it shows you an example of passing a call back function -2nd last example: http://socket.io/docs/#Sending-and-getting-data-acknowledgements

Ex server:

    socket.on('formData', 
              function(data, fn){
                      // data is your form data from the client side
                      // we are here so we got it successfully so call client callback
                      // incidentally(not needed in this case) send back data value true 
                      fn(true);
              }
             );

client:

      socket.emit('formData', 
                  data, 
                  function(confirmation){
                          // send data
                          // know we got it once the server calls this callback      
                          // note -in this ex we dont need to send back any data 
                          // - could just have called fn() at server side
                          console.log(confirmation);
                  }
                 );
like image 174
user3363398 Avatar answered Oct 21 '22 07:10

user3363398


The reason why your second code is not doing anything is because exposed events in socketIO are just defined for socket.on methods. Therefore you need to add another emit in your server app.js to accomplish this

Client emits the custom message and sends JSON data to the socket via socket.emit, also he gets an update function that handles the success callback

socket.emit ('message', {hello: 'world'});
socket.on ('messageSuccess', function (data) {
 //do stuff here
});

Server-side Gets a call from the message emit from the client and emits the messageSuccess back to the client

socket.on ('message', function (data) {
 io.sockets.emit ('messageSuccess', data);
});

You could probably make a module out of this behavior so you can attach this for every message that you want to be handled that way.

like image 27
toxicate20 Avatar answered Oct 21 '22 09:10

toxicate20