Reputation: 77
I am relatively new to Node.js and have run into a weird issue. We have been passing information fine through socket.emit and socket.on until this point. We have a new form that when the user submits, emits with the value of their email, and then hides the login page. The data is being emitted, but for whatever reason it will not go inside the function part of emit to hide the login page. It is not even reaching the if(data) segment.
$agentForm.submit(function(e){
e.preventDefault();
if(validateEmail($agentEmail.val()) && validatePW($pw.val())){
socket.emit('new agent', $agentEmail.val(), function(data){
if(data){
$loginPage.hide();
}
});
} else{
return;
}
});
Values are being emitted and caught on the server, so unsure why it will not go inside the function(data). If more info is needed I can provide. Any help you can provide is appreciated. Thanks!
Upvotes: 0
Views: 698
Reputation: 218
Is your server receiving the connection and invoking the callback function? Seems like you are using socket.io, here's a snippet, if client is run and a connection is not established, or the callback function is not called, it doesn't go into the response handler.
server.js
const io = require('socket.io')({
path: '/test',
serveClient: false
});
io.attach(3000, {
pingInterval: 10000,
pingTimeout: 5000,
cookie: false
});
io.on('connection', function (socket) {
console.log('client connected')
socket.on('foo', function (data, callback) {
console.log(data);
callback('received ' + data); // IMPORTANT must invoke callback
});
});
client.js
const io = require('socket.io-client');
const socket = io('http://localhost:3000', {
path: '/test'
});
socket.emit('foo', 'bar', function (response) {
console.log("response:")
console.log(response);
});
Upvotes: 0