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?
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:
client:
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 thisClient 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
Server-side Gets a call from the message emit from the client and emits the messageSuccess back to the client
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.