This episode is for members only

Sign up to access "Soketi Basics" right now.

Get started
Already a member? Sign in to continue
Playing
05. Broadcasting on private channels

Transcript

00:00
so in order to demonstrate private channels we're going to create out an order model which belongs to a user what we're then going to do is imagine that we had some sort of event on the back end that told us that an order had been dispatched
00:14
and then broadcast that only to that signed in user and then maybe have some sort of pop-up to tell them that the order has been dispatched okay so let's go ahead and focus on our model first of all so let's go ahead and make an order model we'll keep this
00:28
really simple and we'll also make a migration with this as well so let's go ahead and open up create orders table now let's head down and really the only thing we want to do in here is add in a foreign id with that user id and then we'll go ahead and constrain
00:43
that within the database you could add more information in here if you wanted to so we could just say amount for example so we could have an integer in here with the amount let's just go ahead and add that in as well just we have a little bit more information
00:56
okay so we're going to go ahead and run php artisan migrate in here and we are done we now have an order model which can belong to a user we're not going to set any relations up in here because the check for this is going to be pretty
01:09
straightforward now we do this over in the root section and under channels now you'll notice here that there's one already being defined for us which is absolutely perfect for our need what we can do is we can broadcast on this channel name
01:24
so instead of that public things channel we can broadcast on app models user and then the user's id and what's great about this is this channel declaration here within this closure just checks that the id that we've passed in equals the currently
01:39
authenticated user's id so to demonstrate this let's go ahead and create out that event so we're going to go ahead and say php artisan make event and we're going to say order dispatched let's create that and let's head over to the order
01:55
dispatched event we're going to go ahead and again make sure we implement should broadcast just so we know that that's in there and if we come all the way down here to the channel name we're going to now keep this
02:06
as a private channel of course because we want to dispatch this only for that user so that channel name remember over in that channels config was app models user dot and then the currently authenticated or not the currently authenticated user but the
02:23
user who owns the thing that we're broadcasting in this case it's going to be an order so we don't know that at the moment because we're not passing anything through to this order dispatched event so let's just leave that as x for now and let's head back over to
02:40
routes and web and let's go ahead and duplicate this route down and say broadcast slash private this is going to be an order dispatched event and we're going to dispatch this passing in the order itself
02:53
so imagine as an admin you've just dispatched this order and you go ahead and pass this into this event to be broadcast over to the user now we don't actually have any orders in the database at the moment so let's go ahead and create one really quickly
03:06
we're going to assign this to my user really important we'll go ahead and set an amount and we'll go ahead and fill in the created up at and updated at dates in here as well great so we've got an order and of course that's id1 it's the first one we've created
03:21
so when we head over to broadcast slash private this is going to broadcast this on the channel name like we've seen here that we define so we need to accept an order into this so let's go ahead and type in this as order and call this order so that's now part of this payload and
03:38
is also in this class as well so what we can do is we can say well it's the user's id on here user underscore id so we can say this order user id that's it so this is going to broadcast on
03:52
app models user dot one of course because my user id is one if it was two of course it would change and that's pretty much it so now that we've done that we need to go ahead and figure out how we can listen to this on the client side let's
04:07
just figure out and just make sure that this works first of all so let's go over to slash private and there we go that's been broadcast of course nothing's been picked up here just yet so over to dashboard.blade.php
04:22
let's go ahead and again listen to another channel this time it's going to be slightly different because we're listening on a private channel so instead of echo.channel we use echo.private and in here we just go ahead and pass in
04:36
the name of the event app models user dot and now we need to figure out the user's id now depending on the framework you're using or whatever we're in blade at the moment so we can just go ahead and say auth id and that's just going to pop the
04:52
currently authenticated user's id in there but of course you can grab this information from pretty much anywhere depending on what you're using so we're going to go ahead and listen here for the order dispatched event and we're going to go ahead and once
05:08
again get that event information back and just console log out on that okay let's see if this works so let's give this page a refresh and yeah so auth manager id let's just make sure that's a method that we call and there we go okay i'm going to go over and hit refresh on here
05:26
and it looks like nothing has happened now there's a good reason for this if you check out the console here or more appropriately the network tab and we give this a refresh you can see that we're trying to make a request to broadcasting slash auth this is actually an xhr request to
05:43
authenticate this user so it can be mapped up to that authenticated channel now the reason that this doesn't exist at the moment this route doesn't exist at the moment is if we head over to config an app what we need to do is come ahead or go ahead and enable the broadcasting
05:58
service provider so i'm just scrolling down here but of course you can search for it let's go down here and enable this broadcast service provider that is going to go ahead and register this route which will then go ahead and send a request back to the
06:13
back end get a specific token which means that we can listen on that private channel now when we give that refresh notice we get a 200 and we are now technically authenticated with that channel that is defined over in our channels under roots so
06:30
now we can head over give this a refresh come over and there we go there is our order with all of that information only broadcast to this specific user now we can go ahead and demonstrate this by going ahead and logging out and registering a new account so let's
06:47
go ahead and register another account in here and now that we're into our dashboard let's just refresh here and you can see that we don't get that broadcast because it does not belong to that user we're not authenticated to listen on that specific channel that we're
07:02
broadcasting that specific order from which remember we're dynamically setting when we go ahead and set up the channel name here so it doesn't come through to any other user but the person that owns that order so there we go
07:16
really really easy again once we've got everything configured to broadcast on a private channel with a specific user id these channels can be pretty much anything you need they're all over in the laravel docs if you want to go ahead and check them
07:29
out but there we go we have gone ahead and set up both public and private events with sockety very very easily running that in our command line and just broadcasting as usual with laravel
5 episodes 26 mins

Overview

Learn to broadcast events in realtime with Soketi, a simple, fast, and resilient open-source WebSockets server.

In this course, we'll set up a Soketi server, configure Laravel for Soketi, and create events for public and private channels. Then, we'll listen for these events on the client and dump the payload from the server.

If you need realtime events in your app, this course will get you up and running in no time.

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

Episode discussion

No comments, yet. Be the first!