Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

longestdrive's avatar

BroadcastAs and BroadcastWith appear to not be working

Hi - this is linked to getting pusher events working on my app and trying many things to resolve.

In my event class I've tried renaming an event that uses the trait shouldBroadcast. I've set a value in the event using the broadcastAs() method and also some data (for testing) in the broadcastWith() method.

When the event fires to pusher I don't see the correct name or the additional data in what's sent to pusher as I thought it would.

I've tried to look through the api documentation to see how these methods work and struggling to find the right reference but all the guides seem to suggest the methods are there to be used.

That then has led me on the path of whether I've completed upgrades properly. The app started 10 years ago using Laravel 5.1 and has evolved now to using Laravel 10. Each upgrade I've followed the guidance but I don't see many references to events and broadcast that could break the use of broadcasting - plus I know it works as I can see the event in pusher.

Here's my event:

Why would the event ignore broadcastAs and broadcastWith? and where in Laravel are these methods used (so I can do some more research)

Thank you

0 likes
2 replies
LaryAI's avatar
Level 58

To address the issue with broadcastAs() and broadcastWith() not working as expected in your Laravel event, let's go through a few steps to ensure everything is set up correctly.

1. Verify Event Configuration

First, ensure that your event class is correctly implementing the ShouldBroadcast interface and that the methods broadcastAs() and broadcastWith() are properly defined, which seems to be the case from your code snippet.

2. Check Broadcasting Configuration

Ensure that your broadcasting configuration is correctly set up in config/broadcasting.php. Specifically, check that the pusher configuration is correct and that you have the right credentials and cluster settings.

3. Verify Pusher Setup

Make sure that your Pusher credentials in your .env file are correct:

PUSHER_APP_ID=your-app-id
PUSHER_APP_KEY=your-app-key
PUSHER_APP_SECRET=your-app-secret
PUSHER_APP_CLUSTER=your-cluster

4. Check JavaScript Listener

Ensure that your JavaScript client is listening for the correct event name. Since you are using broadcastAs(), the event name will be button.clicked instead of the default class name. Your JavaScript should look something like this:

Echo.channel('channel-name')
    .listen('.button.clicked', (e) => {
        console.log(e.message);
        console.log(e.dateSent);
    });

Note the dot before button.clicked in the listen method, which is required when using broadcastAs().

5. Debugging

  • Logging: You are already logging information in your event class. Check your Laravel logs to ensure that the logs are being written as expected when the event is fired.
  • Network Requests: Use your browser's developer tools to inspect network requests to Pusher and ensure that the event data is being sent correctly.

6. Laravel Version Compatibility

Since your application has been upgraded from Laravel 5.1 to Laravel 10, ensure that all packages, including Pusher and Echo, are updated to versions compatible with Laravel 10.

7. Clear Cache

Sometimes, cached configurations can cause issues. Run the following commands to clear any cached configurations:

php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan cache:clear

Conclusion

If after following these steps the issue persists, consider creating a minimal example to isolate the problem. This can help determine if the issue is with the event setup or elsewhere in your application. If you continue to experience issues, reviewing the Laravel documentation on broadcasting and the Pusher documentation may provide additional insights.

longestdrive's avatar

I tried the suggestions above but no difference.

I decided to create a new event with a different name and essentially the same functionality and it works as expected including the broadcastWith and broadcastAs - makes no sense

Please or to participate in this conversation.