Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add ability to set backoff in broadcast events #42737

Merged
merged 1 commit into from
Jun 10, 2022

Conversation

jpbalda
Copy link
Contributor

@jpbalda jpbalda commented Jun 9, 2022

This PR allows to set a backoff to broadcast events. This is useful when the websockets server is unstable and you want to give the server some time to recover.

@taylorotwell
Copy link
Member

How would this be customized by the end user?

@jpbalda
Copy link
Contributor Author

jpbalda commented Jun 9, 2022

You can customize it in the broadcast event class as a public property. Currently, you can only set $tries and $timeout properties, so the PR is adding another property to customize the job behavior.

Using the docs example, it'd look something like this:

<?php
 
namespace App\Events;
 
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
 
class ServerCreated implements ShouldBroadcast
{
    use SerializesModels;
 

    public $tries = 3;

    public $backoff = 15; // <--- new property


    /**
     * The user that created the server.
     *
     * @var \App\Models\User
     */
    public $user;
 
    /**
     * Create a new event instance.
     *
     * @param  \App\Models\User  $user
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }
 
    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('user.'.$this->user->id);
    }
}

@taylorotwell taylorotwell merged commit 7afdaf1 into laravel:9.x Jun 10, 2022
@GrahamCampbell GrahamCampbell changed the title Add ability to set backoff in broadcast events [9.x] Add ability to set backoff in broadcast events Jun 10, 2022
@jpbalda
Copy link
Contributor Author

jpbalda commented Jun 13, 2022

Thanks for merging my first PR 😃

@jpbalda jpbalda deleted the backoff-for-broadcasting branch June 13, 2022 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants