This episode is for members only

Sign up to access "Soketi Basics" right now.

Get started
Already a member? Sign in to continue
Playing
02. Installing and configuring Soketi

Transcript

00:00
A good place to start would be the Sockety client itself.
00:03
Let's go ahead and install this globally with npm, run it and just see what happens. So let's do an npm install and for this we're going to use the global flag because we want to be able to run this pretty much anywhere on our machine. So we're going to go ahead and say Sockety slash Sockety and just wait for that to finish.
00:22
Okay so now we've done this let's clear the terminal out and let's run Sockety and start and there we go. So our server is up and running. If you get an error at this point and you have a node version mismatch, for example
00:36
at the moment if we just cancel this off and I run node v, I'm running the latest stable release, at the time of recording the non-stable release will be version 18, you may see an error. So I'd recommend you downgrade that.
00:50
If you need to do that here's a little tip, I recommend going ahead and doing an npm install globally on this n helper which I already have installed and then what you can do is you can just run sudo n and then the version you want. So for example you can see that I'm running node version 16, 15, 0.
01:08
You would just go ahead and run sudo n and 16, 15, 0 and that would go and downgrade it or upgrade it for you. Okay so we can go ahead and bring back that Sockety start command and our web server or our socket server is up and running.
01:23
Now let's make a note this is running on port 001. We're going to need to know that because we're going to need to configure that within our Laravel app and we have this usage endpoint available on 9601. So if we go ahead and just open this in our browser, so let's go ahead and just grab this
01:41
link here and just duplicate this across, pull this in, you can see we get an okay on the actual port on the 6001 port but 9601 if we just switch this over to 9601 and then slash usage we can actually see a little bit more information about this. We don't get full statistics on this like some solutions but we at least get some of
02:05
this information. So if you need that there it is. So with this done we want to be able to figure out how to broadcast events from Laravel. You might have done this already but essentially if we just head over to config and broadcasting
02:20
we have a couple of options here that we can use. Now we're going to be using this via pusher. So we're going to be using the pusher SDK to push these instead of pusher which is a third party service over to our Sockety server.
02:33
So we're going to need to come down here to this connection setting under pusher and go ahead and just change around a few of these settings. By default this will be pushing all of our broadcasted events over to pusher which is not what we want.
02:48
So before we start to use pusher or Sockety as a replacement here we want to go ahead and install the pusher PHP library. Let's go ahead and grab the composer command for this and we'll go ahead and pull this in here so that's done and we're going to come over to our EMV while that's working
03:05
and we're going to look for broadcast driver. At the moment this is set to log so that's going to log these to our log files when we broadcast. We want to change this over to pusher.
03:16
Now inside of our EMV file we have some pusher app ID, app key, app secret and cluster variables but now what we can do is just switch these to any values really it doesn't really matter what we use in here. So we could change this to app ID, app key and app secret since we're not actually doing
03:36
this on a third-party server or a third-party service. So what we also want to include in here is our pusher host because of course we're not going to be pushing to the pusher service itself we're pushing to our local machine. We also want to go ahead and configure the pusher port as well.
03:55
Now we know from earlier if we just open up our terminal this is running on 6001 so that's going to be 6001. So now when we broadcast within Laravel a real-time event which we can pick up from our client instead of broadcasting this to pusher we're broadcasting this to 127.0.0.1
04:13
on port 6001. We're also going to include the scheme in here and we're going to set that to HTTP for local development but in production this is likely going to be HTTPS. Okay so we can come over to config and broadcasting and we can update some of the settings that
04:29
we've got in here just to get this working with Sockety. So the host if we just go in under our options we need to define so let's go ahead and define this out in here without env and that's going to be pusher host these are just ones that we're adding.
04:44
I'm going to set default on that as well just in case we forget to include that and we're going to come down here and we're going to change this port over as well so we can change this to pusher port and once again we'll just set a sensible default here of 6001.
04:59
We also want to define that scheme in there as well so let's add that scheme that we defined that's really important scheme and we're going to set that to a default of HTTP and then we want to go ahead and not always use TLS we want to do that only when the pusher scheme is equal to HTTPS there's no need to use TLS if we're just using HTTP.
05:25
Now we don't really need this cluster in here so you could comment this out or get rid of it if you want there's no real harm in leaving it in there because Sockety doesn't have the concept of clusters we also want to set encrypted in here to true and we are pretty much done now with our configuration that's pretty much all we need to do so now
05:45
what we need to do is just set up an event that we can broadcast within Laravel so we're going to go ahead and run php artisan make event and let's just call this something really generic like new thing available so imagine that you are broadcasting to everyone within a public channel that something new was available okay so we're going to go ahead and open up
06:07
a new thing available which is an event and by default Laravel includes the broadcast on method which is going to give us by default a private channel here so I'm going to switch this over to just channel because at the moment we're going to talk about public channels and we're just going to say things so we're listening on or we're pushing to a things
06:29
channel which we'll pick up on the client side so let's just say that within this event we got through a string in here which was a message so we can broadcast this with the payload so what we're doing is by default when we invoke or broadcast this new thing available event this is going to set to a public property within this class and that's going to be
06:52
available in the payload of the thing that we're broadcasting now that's pretty much all we need to do we're not going to go too much into events here the last thing that we need to do that we always forget to do is implement should broadcast that's going to allow this to actually be broadcast so to test this out we can either do this through the command line or what I like to do is just
07:12
create out a new route in here which we can just hit within the browser to go ahead and broadcast this so let's go ahead and just say get broadcast and create just a closure in here for now so we're going to go ahead and say new thing available and we're going to dispatch this with a message so let's go ahead and say it works now eventually what we're going to do is when we hit this broadcast
07:35
route remember this could be an event like someone's just done something posted a comment this event is going to go over to sockety and then over on the client side with our app within javascript we're going to listen to this event to be pulled in so if you're new to real time some of this might seem a little bit confusing but we'll pull it all together and it should make sense
07:55
by the end okay so if we just head over to broadcast in the browser right now and just wait for that to finish and it looks like we've got a pusher error here so let's just head over to here to make sure everything looks good and yeah i've written posts there so you've probably noticed that that of course should be poor let's go back over and give that a refresh and there we
08:15
go that looks like it's been broadcast and that has gone through to our sockety server ready to be picked up on the client side so we've pretty much got everything working now now we need to head over to the client we need to get everything set up there to listen to events on this channel that we are broadcasting from
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!