walkor / phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
2.3k stars 508 forks source link

Should the emit callback work? #255

Open Demo-Nick opened 3 years ago

Demo-Nick commented 3 years ago

I'm trying to do something like:

$socket->emit('test', function($data) {
   var_dump($data);
});

And then on client side (v2.3.1):

socket.on('test', (callback) => {
    console.log(callback);
    callback('CALLBACK WORKING');
});

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).

DanEscudero commented 3 years ago

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);