Socket.io: client-side callback does not work

Chatting with socket.io just to prove the concept still works fine, but I can't get the emit callback to work on the client side. I need to miss something stupid, but the documentation is not a killer at the moment. The server captures the getSomeData event just fine, no errors anywhere.

From what I could say in the source of client socket.io, it checks to see if the last argument for emitting is a function and always uses it as a callback, but debugging any deeper than that was problematic for me.

I feel like I am missing a basic concept. Is this not what this.send(..) should do? I could only find 1 use in the sample applications, and not one of them where client-side code was available for this event.

Update: just to be clear, I actually intentionally emit the client side of the event. The purpose of this was to find out if socket.io can be used so that clients can retrieve data on demand in addition to receiving clicks.

Server:

 var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.on("getSomeData", function() { this.send({data: "some random data"}); }); }); 

client: (console.log never happens)

 <script type="text/javascript" src="http://localhost/socket.io/socket.io.js"></script> <script type="text/javascript"> var socket = io.connect('http://localhost'); socket.emit("getSomeData", function(data) { console.log(data); }); </script> 
+6
source share
3 answers

It looks like you have some kind of logic, try ...

 var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit("getSomeData",{data: "some random data"}); }); 

and the client ...

 <script src="http://localhost/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on("getSomeData", function(data) { console.log(data); }); </script> 

EDIT:

 var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.on("getSomeData", function(name,fn) { fn({data: "some random data"}); }); }); 

Client

 <script src="http://localhost/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.emit("getSomeData", function(data) { console.log(data); }); </script> 
+19
source

In a letter to Liam William, I discovered that you need to send a callback (or confirmation function) as the third argument on the client:

Client:

 <script src="http://localhost/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.emit("getSomeData", null, function(data) { console.log(data); }); 

+5
source

You can have a callback, but you have to do it a little differently:


Server: (app.js)

 var io = require('socket.io')(80); io.on('connection', function (socket) { // please note that server will take 2 data entries as function parameter below socket.on('ferret', function (name, fn) { fn('woot'); }); }); 

Client (index.html)

 var socket = io(); // TIP: io() with no args does auto-discovery socket.on('connect', function () { socket.emit('ferret', 'tobi', function (data) { console.log(data); // data will be 'woot' }); }); 

Actaully, socket io also mentions this; sending-and-getting-data- (acknowledgements)

0
source

Source: https://habr.com/ru/post/893117/


All Articles