Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
Code Racer is a realtime game played by multiple people in different browsers. In order for the game to work we need to be abile to communicate to and from the server very quickly. Web Sockets allow us to do just that, and Socket.io is a library that makes Web Sockets a snap.
-
0:00
[Code/Racer: Installing Socket.io]
-
0:06
So far we've been able to get node up and running,
-
0:08
creating a just basic web app that's able to serve different pages.
-
0:12
We have some of our views over here, like our index.eco,
-
0:16
and they are serving some just very basic files right now.
-
0:19
If we go to our browser, we can see if we just visit the root here,
-
0:22
we have a page with some dynamic content in it.
-
0:26
The next thing I want to do make sure that this game is going to work
-
0:29
is figure out a way for the players to communicate with each other.
-
0:32
Because we have a game and there's a lot of stuff going on,
-
0:34
We need to make sure that every time a player takes an action
-
0:37
that that action is sent up to the server so the server can process that move
-
0:41
or the change or the update in the game state as well as notify all the other players
-
0:46
who are playing in that game so everybody can stay in sync as to what's going on.
-
0:51
As people are typing, we want to be able to send updates about what they've typed,
-
0:55
so other people can see what they've typed.
-
0:57
If we win a round, we need to send a notification from that player to the server,
-
1:01
and that server needs to be able to push down information to all the other clients,
-
1:05
saying that the round is over or this player has one or a weapon has been used
-
1:10
or some other action.
-
1:12
Now, in typical web applications, we worry about just request and response,
-
1:15
meaning that the only way communication can really happen
-
1:19
is when the browser makes a request on the server and the server responds.
-
1:23
If the server has information it wants to push out to one of the actual clients or browsers,
-
1:28
it can't really do that.
-
1:30
It doesn't have a way in a normal HTP environment to push information down.
-
1:34
It just has to wait for one of the clients to make a request.
-
1:37
Then it can give us an update.
-
1:39
But we need something more real time.
-
1:41
We need a way for our server to send out information.
-
1:44
That's where web sockets come in handy.
-
1:46
We're going to be using a tool called "Socket.IO" to handle this.
-
1:50
You can find out more information at Socket.IO,
-
1:53
and there are some good examples here.
-
1:55
It's pretty simple to set up. There are two parts of it.
-
1:58
There's a server component, which will actually handle all of the clients who are
-
2:02
connecting over this socket, and then on the browser there's a piece of JavaScript
-
2:06
where each of the clients has the actual way to communicate to the server.
-
2:10
The way it works is when the client loads a page
-
2:13
they'll open up a socket, and our server is going to receive that call from a socket,
-
2:18
and it's going to hold that line open.
-
2:20
As long as the server has that socket connection and the client is still listening to it,
-
2:25
the server can send information down to the browser that the browser can then handle.
-
2:30
Now we have pretty much what's like a phone call.
-
2:32
Both the client and server are able to communicate with each other whenever they want
-
2:36
as long as that Socket connection or phone call is open.
-
2:39
Let's see how easy it is to get Socket running in our application.
-
2:42
Right now we have our server running, which we started up with supervisor server.coffee.
-
2:47
Now I'm going to stop our server, because we're going to be updating our package.jsan file,
-
2:52
which defines the dependencies of our application.
-
2:55
I'm going to flip back here and open up our package.jsan.
-
2:58
We can see the dependencies we're using right now, coffee-script for our coffee script compiler.
-
3:04
"Express" is our server, "eco" is our template,
-
3:07
and we're using a middleware called "connect-less" to handle the lessCSS that we use.
-
3:13
To add another dependency, what I'm going to do is I'm just going to add "socket.io."
-
3:19
That's the name of the package.
-
3:21
Notice I added a comma on the line before because we need to have commas
-
3:24
between each of our dependencies.
-
3:26
As the value of this, again, I'm going to use star.
-
3:28
I don't know what the current version of socket.io is,
-
3:31
but right now I'm just going to use the latest.
-
3:33
Before long we'll go ahead and actually add the real version numbers,
-
3:37
so we can have a slightly better definition of what our dependencies are,
-
3:39
and we don't have to worry as much about it break if one of these libraries updates
-
3:44
and makes some sort of change.
-
3:46
But for now the star will work, because we're going to just work with whatever the latest version is,
-
3:49
and then we'll lock it down later.
-
3:51
I'm going to save the package.jsan, and then to add this dependency into our project
-
3:56
from our Code Racer directory, I'm going to run, once again, npm install.
-
4:04
Now node has installed socket.io and all of its dependencies,
-
4:08
so we're able to require socket.io from our application.
-
4:12
I'm going to just start up our app again by typing in supervisor server.coffee.
-
4:18
Remember, supervisor is the application we're using to restart our application
-
4:22
every time we either save a file or whenever the application crashes
-
4:26
because of an error.
-
4:28
Supervisor will keep our app up and running so we don't have to keep coming
-
4:31
back to the terminal to stop and restart it for new code changes.
-
4:35
We start it up, and we get our console.log message saying our server is listening.
-
4:40
We should be able to flip back to our page and refresh,
-
4:44
and we still get our special title.
-
4:47
Now, the way we set up our socket.io server is we need to initialize a socket.io server,
-
4:53
and there are two different ways we could do this.
-
4:55
We could create special socket.io server and have it listen on its own port,
-
5:00
but socket.io is also compatible with Express, which is our web server.
-
5:05
We can actually have socket.io hook into our existing Express app,
-
5:10
which we have in our app variable here.
-
5:12
It will attach all the functionality to our server that we already have up and running.
-
5:17
What I'm going to do is I'm going to store our socket.io instance into a variable called "io."
-
5:23
The way we get that is we will require socket.io,
-
5:30
and this imports the socket.io object.
-
5:33
What we can do is call the dot-listen method that socket.io provides.
-
5:38
If we were starting up our own server, we could just do something like port 4000,
-
5:44
and it would server the server on port 4000.
-
5:47
But since we want to hook it into an existing Express application,
-
5:50
instead of passing in a port number, we'll pass our actual application.
-
5:54
Now socket will know that we have an express application
-
5:57
and we'll be able to listen for socket connections that come
-
6:00
over our normal connection here on port 3000
-
6:03
as well as it serves a couple extra pages, including the client JavaScript file that we need.
-
6:09
If I save it out, we should see our server has restarted, because I saved the file,
-
6:14
and if we go back to a browser and refresh we don't see any changes.
-
6:18
The one way we can test is to see if it's serving the socket.io client file
-
6:22
that we're going to need to include in our page to actually communicate with our server.
-
6:27
We can check this out, and the place that it will serve this file is from /socket.io/socket.io.js,
-
6:38
and if we pull that up, we can see the socket.io now is hooked into that URL.
-
6:44
When we request it, it'll return the JavaScript file that defines the client of our application.
-
6:51
So what we need to do next is to include this into our page.
-
6:55
I'm going to keep working on our demo page that we have in our index.eco.
-
6:59
We will go back over here, and remember index.eco right now is just the inside of our page.
-
7:05
What we actually want to modify is layout.eco,
-
7:08
and this is where our head tag is where we have our style sheets
-
7:10
and where we can put our script files.
-
7:13
What I'm going to is I'm going to create a script tag,
-
7:17
and the source of it will be that URL that we were talking about before--
-
7:20
/socket.io/socket.io.js.
-
7:29
Close the script tag, and we got it .
-
7:36
If we go back to our browser and we go back to our home page, not much has changed,
-
7:42
but I'm going to open up my JavaScript inspector here,
-
7:45
and we can actually see that one of the scripts is loaded up as socket.io.js.
-
7:49
We can test it out by typing "io," which is where it stores all of the information.
-
7:55
We can do io.connect, and this is the call that will actually connect from our client to our server.
-
8:02
This is like making the phone call or the connection from our browser to our server.
-
8:07
If we do that, we can see in our debug here
-
8:12
we see our server is listening. This is the last time we restarted our server.
-
8:16
We can see that it's also telling us how it serves /socket.io.js,
-
8:21
so it's giving us some information there.
-
8:23
All this information is coming from socket.io.
-
8:25
Since it's hooked into our server, it's able to handle requests that it's going to handle,
-
8:30
mostly things as /socket.io, and we can see what's going on with our socket.
-
8:34
We see that we got our client authorized, which means we did a connect call,
-
8:38
and it got authorized, and there is even a handshake to make sure the server and client
-
8:42
are all synced up and ready to talk to each other.
-
8:45
Now, we can see some more information about how it's actually
-
8:48
handling the information back and forth.
-
8:50
We can see that there's a heartbeat interval, which is being sent by the server
-
8:55
or being checked by the server to make sure that our clients are still connected.
-
8:58
A lot of this we can ignore as we develop, but it's a good log of what's going on with connections.
-
9:03
Basically, just by doing that one connect call we had a handshake,
-
9:06
we're getting some heartbeats here just to make sure that we're up and running,
-
9:10
The next step that we're going to do is actually work on communicating
-
9:14
back and forth from the client and the server.
You need to sign up for Treehouse in order to download course files.
Sign up