question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Laravel broadcastAs and Echo listen mismatch

See original GitHub issue

Hi,

Laravel gives the possibility to name event’s broadcast, however, Echo assumes that the event’s name is always a namespace. Following this documentation:

public function broadcastAs()
{
    return 'server.created';
}

I was trying to broadcast the event via Pusher, and had this code in my Javascript:

window.Echo = new Echo({
    // settings
});
window.Echo.private(`App.User.${user.id}`)
.listen('.server.created', (e) => {
    console.log('notif arrived', e);
})

But wasn’t receiving any notifications in the front end, the problem was in Echo’s event-formatter, the format method replaces all . with \\

format(event: string): string {
        if (this.namespace) {
            if (event.charAt(0) != '\\' && event.charAt(0) != '.') {
                event = this.namespace + '.' + event;
            } else {
                event = event.substr(1);
            }
        }
        // Why is this here?
        return event.replace(/\./g, '\\');
    }

Now I’m not sure if the problem is:

  1. is it in Laravel Event broadcastAs method that should format the event’s name similar to Echo?
  2. or Echo shouldn’t always format the event name and allow the developer to choose? which seems to be the right way to do it, unless I’m missing something. I can PR this.
  3. or Laravel documentation is incorrect?
  4. or I’m missing something.

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ellisiocommented, Jul 25, 2017

Seeing as how namespace is exposed in Echo’s options, it might be worth changing the documentation to reflect that doing the following will prevent App\\Events\\ from being prepended.

let Echo = new Echo({
    namespace: null
});

Then changing the formatter to the following:

format(event: string): string {
    if (this.namespace) {
        event = this.namespace + '\\' + event;
    }

    return event;
}

Finally, change connector.ts’s namespace: 'App.Events' to namespace: 'App\\Events' since Laravel’s BroadcastEvent does get_class($this->event) here.

With those changes you could have the following configuration:

Dot Syntax

// PHP
broadcastAs() {
    return 'server.created';
}

// JS
let Echo = new Echo({
    namespace: null
});

Echo.listen('server.created', () => {});

Default Laravel Namespacing

// PHP
namespace App\Events;
class ServerCreated extends Event { }

// JS
let Echo = new Echo;
Echo.listen('App\\Events\\ServerCreated', () => {});
1reaction
hernandevcommented, Jun 5, 2017

made me crazy for a couple of hours.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel echo listen for broadcastAs namespaced event
I'm trying to figure out how to listen to the event from the custom namespace, but with the event name defined by the...
Read more >
Laravel Echo not listening - Stack Overflow
try to set broadcastAs in your event class and explicitly set the broadcast name in reactjs part. App\Events\PostCreated
Read more >
Broadcasting - Laravel - The PHP Framework For Web Artisans
Laravel Echo is a JavaScript library that makes it painless to subscribe to channels and listen for events broadcast by your server-side broadcasting...
Read more >
Complete guide to achieve WebSocket real-time ... - Medium
Here we use the official Laravel Echo extension package to listen to the server broadcast. Open resources/assets/js/bootstrap.js and modify the last few lines....
Read more >
Laravel Echo Vuejs Not Hearing Pusher Event - ADocLib
Laravel broadcastAs and Echo listen mismatch #119 Now I'm not sure if the problem is: Event not being triggered by Pusher. #73.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found