Coder Social home page Coder Social logo

Comments (15)

tlaverdure avatar tlaverdure commented on May 22, 2024

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

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

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.

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

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

from laravel-echo-server.

tlaverdure avatar tlaverdure commented on May 22, 2024

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.

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

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?

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

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

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

@tlaverdure

https://github.com/laravel/framework/blob/6cf003ae02582cf716c10a723ef6bc19690969e7/src/Illuminate/Broadcasting/PendingBroadcast.php#L46-L58

toOthers() just runs dontBroadcastToCurrentUser() haha I'm so confused now..

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

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

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

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

from laravel-echo-server.

 avatar commented on May 22, 2024

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

from laravel-echo-server.

feenx avatar feenx commented on May 22, 2024

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

from laravel-echo-server.

lin4you avatar lin4you commented on May 22, 2024

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

from laravel-echo-server.

ctf0 avatar ctf0 commented on May 22, 2024

incase someone is having the same issue,

from laravel-echo-server.

eddyson1006 avatar eddyson1006 commented on May 22, 2024

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();
......
}

from laravel-echo-server.

eddyson1006 avatar eddyson1006 commented on May 22, 2024

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

from laravel-echo-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.