Learn to Code via Tutorials on Repl.it!

← Back to all posts
Online Multiplayer in NodeJS
LeonDoesCode

Multiplayer

So you've decided to venture into the realms of Online Multiplayers. Why not? You can get all you friends involved in the projects you make, or just show it off to others. Well, either way, let's take a look at how we would make one.

Modules

Firstly, we are going to need some Modules to help us along the way. Let's see:

That should do the trick! We have:

  • express to display the static site to the Clients
  • http to listen for new Clients
  • socket.io to listen for events the Clients make

Simples! So now that we know what we are working with, let's make this Game!

Client

Here are the HTML and CSS before we begin, as they aren't super important, I won't explain them too much:

index.html

A good thing to not is that you should not forget the <script src="/socket.io/socket.io.js"></script>. this is super important!

style.css

This just makes it look nice. It also fills the background with the same as the canvas, and center said canvas in the middle of the window.

I'd say that the Client is always best to work on first. The reason for this, is that you can begin to layout what you are going to be using. This means you know what you will need on the Server before you start writing it. Obviously you will be adding stuff later, but it's a great way to begin the project. So let's begin:

Firstly, we want to get our Client socket to communicate with the Server. Now we can create the setup function:

This is nothing special. All we do is define our setup function and createCanvas() a 600x600 canvas. We also remove the outline around our players by using noStroke():

Next we define our W, A, S, and D keys. The numbers 87, 65, 83, and 68 refer to each key's code. A good way of finding out a key's code is by using this website. All you have to do is press the key you want to get the code of, the nice thing is that the favicon of the page will change to the code's number so that it easier to see when you switch tabs:

Now we get into the nitty gritty. We create an event called update which we will talk more about in the Server section. This event will give us the Game's current data to update the Client-Side with. talking about that:

We change the background color to 50 using background(). We then check if either of W, A, S, and/or D is down, if so, we send an event to the Server called move. We also send the key with it. Next:

We get all of the ids for all the players in data.players, we then get the player for the id and show them on the canvas. This allows us to see all the players. But what about us?

I'm glad you asked! Well, we do draw ourselves here, we do it along with the others. Because all the updating is done Server-Side (which may change depending on how you implement it), the only thing we do is get movement events, and draw the players on the Client-Side.

This is it for the Client-Side of things. Super simple! So let's get onto the Server-Side of things!

Server

The server will now have to do a few things based on how we wrote the Client. These include:

  • Sending an update event to the Clients
  • Receiving move events from the Clients
  • Updating the players

Not too much, so let's get cracking! Oh, and we covered Socket.io in another tutorial, so I won't be diving to far in in this one:

We first want to make it so that the Client is given the Game website. Then we can:

We define a few variables and constants. These include the movement keys from earlier, the data including the players, and the width and height. Not too much so far:

Firstly, we get the connection and disconnect events. When we get a connection event, we create a new player and give it the socket.id. On the disconnect event, we delete the player from data.players using the socket.id:

Next we get the move event. In this event, we check to see if any of the movement keys are pressed. We do this, just in case the Client sends us something which is not a movement key, think of it as extra security. If one of the keys is pressed, we add or subtract the player.speed from the player.pos x or y. We then also check to see if the player is at one of the boundaries. If so, then we stop the player from passing it. That's it!

Conclusion

It's pretty easy when you look at it. You also have the extra security of having things done Server-Side which is nice. The player stuff could be done better, but works great for a little "test" online game. Hope it helps in your ventures!

Have a great day!

P.S
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!

Voters
AndrewMarkarian
JJJ2008
LiLongp
trashto
Spiered
zai0002
hazelpy
KendraJackman
DileepDilkharaj
pahosler
Comments
hotnewtop
[deleted]

If I use a nodeJS repl here on repl.it, it won't show the code, only the nodeJS console. What should I do?

KittyCraft0

@stringycheese if your on phone zoom out and/or scroll down and youo will see some options, im on a chromebook sometimes (like right now) and a windows 10 computer other times and it is working fine for me

edit:
if you want the webpage html you need to do some code first to set the port and to set it to show the html, and from there you can ut the jvvascript into a script part of the html, although sadly it sees you cant make it run another script file, you need to write it all in the html, and also there is no check for syntax it seems. But it seems the page reloads whenever it tries to run a script with wrong syntax, so you can keep an eye out for that.

fghgfhgfhghfhgg

i only see the color of canvas, player no load or objects

Samuelstankiewi

an error is reported on line 39:7, in the JS file. it says that the key codes have already been defined (in line 15-18). For some reason, when I remove the error, it displays the canvas wrong.

TristanMisja

Could you make a tutorial on json-store-client in Python? I could really use it! Thanks! :D

GameWatch21

Wow this is so good thanks for the tutorial tho

dcbriccetti

Thanks for this. Do you have repls with the finished server and client code that we can fork?

LeonDoesCode

@dcbriccetti I do indeed, here it is.

Rileygamer21
ChromeRemoteDes

@LeonDoesCode It gives a 404 error

KittyCraft0

@LeonDoesCode 404 error

broderickCode

Add a comment

KittyCraft0

I am adding a reply @broderickCode

Zavexeon

Awesome tutorial!

A tutorial on how to create a node.js photovault/image host would be neat.

LeonDoesCode

@Zavexeon Coolio! I'll see what I can do!