This episode is for members only

Sign up to access "Building Reactive Realtime Applications with Livewire" right now.

Get started
Already a member? Sign in to continue
07. Broadcasting new posts


By the end of this episode, we'll have real-time updates when someone types a message.
This will be broadcast to everyone else who is currently logged in and on this page, and they will see the new messages roll in. So I have opened up two windows here,
and I'm logged into two different accounts. I've created another account. So when we type something in here, that should then update on this side.
Let's get started and see how we can do this. So we are going to, first of all, create an event. So much like we did before, so phpArtisan makeEvent. And this is going to be called postCreated.
So when a new post gets created, we can broadcast this and listen on all of our other channels or all of our other users. So let's go over to our postCreated event.
Remember, the first thing that we need to do is make sure we implement shouldBroadcast. So let's implement shouldBroadcast in there. We're going to go down because we know
that this is a public channel, and we're going to rename the channel to Posts. Now, the beauty of what we're doing here, because we're working just with batches, with IDs,
we don't need to broadcast the whole object. We don't need to relook the object up. We just need to push the ID in. So we're going to get into this a post ID.
So we're going to receive into this event a post ID. That post ID is then going to be broadcast to all of the other clients that are listening, and we'll just push that post ID
into the stack of the first batch. So now that we've got this event, where do we dispatch this from? Well, we dispatch this when we create a post,
and that is over in our createPostLiveWire component. So it doesn't really matter where we dispatch this inside of here. We could do this last.
We could do it just up here, but as long as we know the ID, that's what we need to do. So let's go and just do this up here, and we're going to broadcast, like we saw before,
a new post created event, and we're going to pass the post ID into this class that we've just created. Now, we're not going to add two others just yet,
because I'm going to show you how this works, but let's take a look at picking this up and seeing what we can do with it. So where do we want to listen to this event?
Well, we want to listen with our main component here, so we can then push it on, like we did with this form that we've got. So if we head over to our post index component,
we know that we are listening for post created here, and then we prepend this post onto here. Now, what I'm going to do, we could combine these, but I'm going to create out a separate method in here,
which is specifically for when we receive a broadcast. So I'm going to create out a method here called prepend post from broadcast, and of course, you can call that whatever you want.
Into that, we get a post ID, so it's very, well, it's exactly the same as this method, but if you needed to do anything specific when you receive a broadcast,
it's better to have a separate method for this. So we're going to listen to that broadcast event, like we've already seen, and that is using echo from our posts channel,
and that is post created. So that will give us that post ID through into here. In fact, it won't, it will give us our payload. I guess that's the whole reason
why we're splitting this out into a different method. So we'll get a payload in here. Let's just die dump on that payload, just so we can see what we get.
So if we come over to here, let's go and just put something in here, and you can see that payload gets pulled in, and we get the ID in here.
So we need to extract that post ID out, and then go ahead and push it to the list. Now to do this, it's pretty simple, because we already have our prepend post method.
So we just want to access the payload, access the post ID, and just forward it over to the method that we've already got, which will push it to the top.
So let's try this out. We'll come over to our browser. I'm going to give both of these pages a refresh, so we get connected up,
and I'm going to write hello, post this, and there we go. Sure enough, that gets pushed into here. Now, what we want to do is broadcast only to other users, because remember, we have our own functionality
when we're on the page within a Livewire component, and we're listening for that just here. We don't want to receive this event in, and this event in, and do this twice.
It doesn't matter that we're doing it twice, because we're working with unique IDs, but we kind of want this just to work for other clients. So to do this, what we do is we chain on to others,
and that will not broadcast that event to us. It will just broadcast it to every other person who's on this page. So the functionality here won't be any different.
Let's just check this out. We'll type something in, post it. It gets broadcast to everyone else, so we could have 100 other people on this page,
and they would all receive this new message in, but it just won't be broadcast to us, because we're pushing this by ourselves. So that is how we do this.
Very, very easy. Once we've got our WebSocket server set up, we just broadcast an event with LiveWire. It's very easy to just listen to that event
and just push this data run. So now that we've done that, let's look at some more complex scenarios, like deleting a post and editing a post.
12 episodes1 hr 43 mins


Livewire can react and re-render anywhere you need it to. But what if we have a infinite scrolling timeline of user posted content with updates in realtime? With added reactivity, we need to be a bit more careful about performance.

Let’s build this together, making sure we keep things running smoothly along the way.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.