Open Demo-Nick opened 3 years ago
emit.on
, in js), the second argument is a callback, which is a function that receives the data sent from server. What you're doing is trying to call this data as a function, which will not work. Try this code: server:
$socket->emit('test', 'Message sent from server!);
client:
socket.on('test', (data) => {
console.log('Message received in client:');
console.log(data);
});
Alternatively, you could try defining this callback and passing it to the listener, like this:
function callback (data) {
console.log('Message received in client:');
console.log(data);
}
socket.on('test', callback);
I'm trying to do something like:
And then on client side (v2.3.1):
But client shows empty object and nothing happening. P.S. In reverse way it works fine (if emit from client and then trigger callback on server).