Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should the emit callback work? #255

Open
Demo-Nick opened this issue Dec 26, 2020 · 1 comment
Open

Should the emit callback work? #255

Demo-Nick opened this issue Dec 26, 2020 · 1 comment

Comments

@Demo-Nick
Copy link

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
Copy link

  • When you emit from server, you should emit either a string or a json. In your case, the second parameter of emit is a function.
  • When you setup a listener on client (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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants