Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Events being duplicated #60

Closed
feenx opened this issue Oct 20, 2016 · 15 comments
Closed

Events being duplicated #60

feenx opened this issue Oct 20, 2016 · 15 comments
Labels

Comments

@feenx
Copy link

feenx commented Oct 20, 2016

So even though I have $this->dontBroadcastToCurrentUser(); in the constructor for a public event it is still being sent to the current user. I've checked and my Echo client is sending the X-Socket-Id header so that laravel-echo-server should be able to filter out events for the current user's socket id but I'm not sure where to start debugging this.

@tlaverdure
Copy link
Owner

When you post to your laravel application with the X-Socket-Id, ensure that it matches the socketID that is being broadcasted to Redis. Maybe try a dd in your broadcasting event.

Here is the relevant code on laravel-echo-server.
https://github.com/tlaverdure/laravel-echo-server/blob/master/src/echo-server.ts#L148-L165

@feenx
Copy link
Author

feenx commented Oct 21, 2016

It is indeed exactly the same.

Here's showing the post request that triggers the event, and I've dd'd the event and you can see the socket id is apart of the event object.

untitled

Then in the tooltip you can see it is the same as the current socket id but I'm still getting the event. I looked at the code you said and it does seem as it would look thru connected sockets for the X-Socket-Id record, and then passes that socket to the toOthers() I just don't see how toOthers() would send it all but that socket, thru that socket. But I admit I'm extremely unaware how socket.io works, especially on nodejs I'm just trying to learn it lol.

@feenx
Copy link
Author

feenx commented Oct 22, 2016

@tlaverdure any ideas?

Just pushed all my latest code to staging env and it is still duplicating events to current user. And I made sure again that the X-Socket-Id header is being correctly sent, and set on the event. The code doesn't seem to filter it out though.

@tlaverdure
Copy link
Owner

Think you may be implementing this wrong on the Laravel side. If both socket ids match on both laravel and laravel echo server then you should be good to go.

When the message is broadcasted to redis is should contain the socket id. With that in hand laravel echo server can broadcast a message as that socket:

socket.broadcast.to(channel).emit(message.event, channel, message.data);

I believe $this->dontBroadcastToCurrentUser(); was depreciated and you need to use ->toOthers() on your broadcast() method.

Example from docs:

https://laravel.com/docs/5.3/broadcasting#only-to-others

broadcast(new ShippingStatusUpdated($update))->toOthers();

Hope that helps.

@feenx
Copy link
Author

feenx commented Oct 22, 2016

Well, I just reread the documentation and I still feel is functioning incorrectly.

The point as I can read is that ->toOthers() is to not broadcast to the current users socket vs broadcasting specifically on that socket like it looks like your code does. And from what you just said, "contain the socket id. With that in hand laravel echo server can broadcast a message as that socket."

Wouldn't broadcasting a message as "that socket" mean only the current user would get it?

@feenx
Copy link
Author

feenx commented Oct 22, 2016

I do understand I could be understanding how socket.io works.

@feenx
Copy link
Author

feenx commented Oct 23, 2016

@feenx
Copy link
Author

feenx commented Oct 24, 2016

@jonnywilliamson perhaps you could shine some light on this for me.

@feenx
Copy link
Author

feenx commented Oct 26, 2016

@tlaverdure so looking through the socket.io client library I see that by using .broadcast() on a socket instance it broadcasts to all other sockets, except the one sending it. Odd usage for it but I see it is indeed not an issue with your package.

Although I still don't know why events are going to the current user as well.

@ghost
Copy link

ghost commented Jan 8, 2018

@pfeiferchristopher I've exactly the same issue with Laravel 5.5 and Vue / Axios You found a solution ?

@feenx
Copy link
Author

feenx commented Jan 10, 2018

Sorry, haven't worked on this project since a few weeks after my last comment.

@lin4you
Copy link

lin4you commented Mar 19, 2018

You have to use the trait InteractsWithSockets. Than the call of dontBroadcastToCurrentUser() method makes sense.

@ctf0
Copy link

ctf0 commented Jun 13, 2018

incase someone is having the same issue,

@eddyson1006
Copy link

Same problem for me.
All socket authentication, broadcast and others are working for me, except for "toOthers()". Somehow, it still broadcast the content to the sender. Let's say the event triggered is by API, how should we link the API to use their X-Socket-ID?

a) Client side
bootstrap.js
import Echo from "laravel-echo"
window.io = require('socket.io-client');
// Have this in case you stop running your laravel echo server
if (typeof io !== 'undefined') {
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'http://mysite.test:6001',
encrypted: true
});
}

welcome.blade.php
window.Echo.connector.options.auth.headers['Authorization'] = 'Bearer ' + 'xxxxxxxxxxxxx';
var channel = '5c36ca48fa6f3f2f280030bf';
console.log('Joining channel: ' + channel + '...');
console.log('Listening to ConversationActivity...');
window.Echo.private(channel)
.listen('ConversationActivity', (e) => {
console.log('New Conversation: ' + e.data.message);
});


b) Server side
............
use Illuminate\Broadcasting\InteractsWithSockets;
..........

class ConversationService
{
......
use InteractsWithSockets;
broadcast(new ConversationActivity($conversation))->toOthers();
......
}

@eddyson1006
Copy link

Just solved my problem by attaching X-Socket-Id in API calls. So it can track back who is the sender based on socket perspective. 👍

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

No branches or pull requests

5 participants