تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Creating the Event0:00

In this video, we'll figure out how to use Laravel's event broadcasting in lieu of using the Redis publish method directly. Okay, so in Laravel, we know that when we want to make an announcement that something just took place, we could either use dependency injection to pull in the event dispatcher, or we could say event::fire, or we could just use the global event function. So if we want to reproduce this section right here that a User signed up, then we could say new UserSignedUp. Let's set that up. php artisan make:event UserSignedUp. Okay, so now you will see this within your app/Events directory right here.

Configuring Redis Broadcasting0:32

php artisan make:event UserSignedUp. Okay, so now you will see this within your app/Events directory right here. But now we don't want to simply fire an event on our server side. We also want to say that we want this to broadcast to the client. And as with a lot of things in Laravel, you have a couple drivers to choose from. So if we go to config/broadcasting.php, you'll see that out of the box, and of course, you could write more if you need to, but out of the box, you could use Pusher, which we've covered here at Laracasts, or we can use Redis, which is obviously the one we'll pick for this series. So I'm going to switch that over to Redis.

Enabling ShouldBroadcast1:11

this series. So I'm going to switch that over to Redis. And now whenever we broadcast an event, we will use Redis to publish. All right, so if we go back to User signed up, you'll see that we reference or import ShouldBroadcast. However, we don't implement that. And that's because, well, some events should not broadcast while others should. So if you want to say, yeah, I want to use Redis, and I want to broadcast this to the client, all you have to do is say implements ShouldBroadcast. And that's it.

client, all you have to do is say implements should Broadcast. And that's it. Behind the scenes, when you fire this event, Laravel will check and it'll say, hmm, okay, you have a new event. Does it implement the shouldBroadcast interface? And if it does, I need to make sure that I use the default driver, which is Redis, to publish that. So now let's figure this out. When we announce that a User signed up, what do we want to pass through? And frequently your event classes could be as simple as a single ID, just a single parameter,

Choosing Event Payload2:01

When we announce that a user signed up, what do we want to pass through? And frequently your event classes could be as simple as a single ID, just a single parameter, or username, or if you want, it could be the full User model. People tend to disagree on what's most appropriate to pass through here, but you know what, just make up your own mind as to what makes sense and everything will fall into place. So in this case, let's say, well, really, we would have some kind of User model here. However, in our case, we don't really have anything set up, so maybe we can just pass through, like I said, the ID of the User, or let's keep it simple and just send through the username who just signed up. Now I can get rid of all of this stuff.

the username who just signed up. Now I can get rid of all of this stuff. And if we switch back, we will accept that username. And the way it works with Laravel is it's going to serialize this and any property that is public will be included with the data that gets published using Redis in this case. So that's important to keep in mind. If you have any protected or private properties, those will not be serialized and those will not be sent through, only the public properties. Okay, so the final thing we want to look at is this section right here, broadcastOn. This will define the channel that we want to broadcast on.

Selecting Broadcast Channel3:09

Okay, so the final thing we want to look at is this section right here, broadcastOn. This will define the channel that we want to broadcast on. So far, we've been using this test channel, and that's fine, but you could have multiple channels or you could have private channels, whatever is appropriate for your needs. Okay, but yeah, that's it. So now if we go back to routes.php, this is that much cleaner. So let's do this. Let's inspect everything. I'm going to go back to my socket.js file and I will console.log the channel as well.

Testing and Debugging Output3:35

Let's inspect everything. I'm going to go back to my socket.js file and I will console.log the channel as well as the message. And that's all we will do for the time being. All right, so let's go into my VM, into our code Redis lesson directory and boot up our server. Now, if we switch over to Chrome and let's give this a refresh, of course, we got class User signed up, not found. I'm sure you saw this. We of course need to import this full path.

I'm sure you saw this. We of course need to import this full path. So app/events/UserSignedUp. Okay, give it one more shot. And if we switch to the terminal, we're still firing on the test channel channel. And now Laravel is sending through an object that contains the event as well as the data. Now remember, this is the serialized event object. So if I go back to Sublime and to UserSignedUp, notice we only had one. So username is included there. If I had a second one, for example, public $age = 30;

So username is included there. If I had a second one, for example, public age equals 30. And if we do this again, refresh, now you will see that included. That's the way this works. Okay, cool. So yeah, this seems to make good sense. If I go back to socket.js, we can still parse the message. And that will give us a object with an event property. We are still passing that through here. And then we also have a data property that contains, again, the serialized user signed.

We are still passing that through here. And then we also have a data property that contains, again, the serialized User signed up object. All right. So let's bring that back. And the only remaining thing to do is adjust our welcome view. Now we're listening for not just User signed up, but the full class path like that. So check this out. Let's console.log the data that we send through here. And if I come back to Chrome, open up Chrome DevTools with shift command C, give it a refresh.

You get the basic idea, right? Okay, so cool. In this video, you learned how to not use the Redis publish method, which you can still do if you need to. But instead, we use Laravel's native event handling to broadcast a typical server-side event to the client.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟