In 0.9.16, I use socket.emit with callback so that the chat server return some data and I can handle the result as per the acknowledgement. But after the upgrade to 1.3.5 I've found a error in console like this
Uncaught TypeError: Cannot read property 'apply' of undefined.
I've done something like this,
From web
socket.emit('userToUser', { 'usename': 'John',
'message': 'hi'
}, function(callback){
//callback handled
});
Chat Server
socket.on('userToUser', function(content, callback){
//do something
if(callback) return callback({'result':'success', 'messageid':content.messageid, 'chatid':content.chatid});
});
When I removed the callback from client side, there is no error. So I believe there will be some changes to be done in the callback.
I'm getting the acknowledgement and the chat is working properly, but my concern is about the console error which leads to socketio.js
Socket.prototype.onack = function(packet){
debug('calling ack %s with %j', packet.id, packet.data);
var fn = this.acks[packet.id];
fn.apply(this, packet.data);
delete this.acks[packet.id];
};
Guys, please help
In 0.9.16, I use socket.emit with callback so that the chat server return some data and I can handle the result as per the acknowledgement. But after the upgrade to 1.3.5 I've found a error in console like this
Uncaught TypeError: Cannot read property 'apply' of undefined.
I've done something like this,
From web
socket.emit('userToUser', { 'usename': 'John',
'message': 'hi'
}, function(callback){
//callback handled
});
Chat Server
socket.on('userToUser', function(content, callback){
//do something
if(callback) return callback({'result':'success', 'messageid':content.messageid, 'chatid':content.chatid});
});
When I removed the callback from client side, there is no error. So I believe there will be some changes to be done in the callback.
I'm getting the acknowledgement and the chat is working properly, but my concern is about the console error which leads to socketio.js
Socket.prototype.onack = function(packet){
debug('calling ack %s with %j', packet.id, packet.data);
var fn = this.acks[packet.id];
fn.apply(this, packet.data);
delete this.acks[packet.id];
};
Guys, please help
Share Improve this question edited May 5, 2015 at 9:33 Tony Jose asked Apr 29, 2015 at 6:25 Tony JoseTony Jose 1,6641 gold badge13 silver badges26 bronze badges2 Answers
Reset to default 6Finally I've fixed the issue. It was a mistake in my code , I've done multiple callbacks in the chat server. like this:
socket.on('userToUser', function(content, callback){
mysql.insertChat(content, function(err, data){
return callback({'result':'1'}) //first callback
})
sendToUser(content, function(errm successData){
return callback({'result':'success','chatid':content.chatid});
//second callback ->wrong
})
});
In the previous versions it was a warning, now its an error !! That's it. So please avoid multiple callbacks
Please have a look at this and might be useful for every nodejs developer: http://www.toptal./nodejs/top-10-mon-nodejs-developer-mistakes/#remote-developer-job
Thanks guys for upvoting !
EDIT: This is not how callbacks work with socket.io v1.3.5. In fact, the emit function does not accept any callbacks at all.
I'm guessing you want to send an acknowledgement to the sending node that its message has been received. To acplish that, you need to make another socket.emit
call from the server (on the server, socket variable represents the connection to a specific node in the network whereas on the client, the socket variable represents the connection to the server). Consequently, on the client, you need to handle the acknowledgement with a socket.on
which is where you'll put your callback.
To broadcast the acknowledgement to ALL nodes in the network, you should use io.emit
.
Code sample for a chat application using socket.io is available here.