Laravel broadcastAs and Echo listen mismatch
See original GitHub issueHi,
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:
- is it in Laravel Event
broadcastAs
method that should format the event’s name similar to Echo? - 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.
- or Laravel documentation is incorrect?
- or I’m missing something.
Thanks
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Seeing as how
namespace
is exposed in Echo’s options, it might be worth changing the documentation to reflect that doing the following will preventApp\\Events\\
from being prepended.Then changing the formatter to the following:
Finally, change
connector.ts
’snamespace: 'App.Events'
tonamespace: 'App\\Events'
since Laravel’sBroadcastEvent
doesget_class($this->event)
here.With those changes you could have the following configuration:
Dot Syntax
Default Laravel Namespacing
made me crazy for a couple of hours.