How To Use Socket - IO and Build Your First Multiplayer Game! - Geeklore - Io
How To Use Socket - IO and Build Your First Multiplayer Game! - Geeklore - Io
io
Geeklore
Press ⌘ K to search the vast kn Log In
Kuberdenis
May 15, 2024
Prerequisites:
Must-haves
node.js
npm
express
socket.io
VS Code
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 1/13
Introduction
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
Backstory
If you read my other post you already know what we are going to build. If not - let me
explain. We are going to build a version of ChrisOnCode's 'Crazy Buttons' lesson. We
are going to create a button which when clicked, moves to a random location within
the document. We are also going to make the movement real-time so other people
see if there is somebody else clicking it.
What is socket.io
As stated by the developers themselves: "Socket.IO is a JavaScript library for
realtime web applications. It enables realtime, bi-directional communication between
web clients and servers. It has two parts: a client-side library that runs in the
browser, and a server-side library for Node.js"
So basically it's a library that allows us to create realtime applications. An example of
such is the .io games and the Facebook chat. I suggest you take a look at their official
website. Checking this link could also help you understand how it works.
Now you know what we are going to build as well as with what.
Setting up the environment
As I am using Windows, I will be using Windows terms but if you're using another OS I
am sure you will manage. Okay, we will start by setting up the environment. Create a
new folder and name it The internet button. Open VS Code in the folder and create
the following structure:
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 2/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
Now we need to build the fundamentals of our application. Open a terminal in the
directory and type
npm init
Leave the values blank, or add your name and description - it's your choice. We also
need to install nodemon so we don't have to restart our application each time we
make a change. Do so with
npm install -g nodemon
After all the package installation, your package.json should look similar to this:
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 3/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
Okay, now let's set-up the express server. We will first set our path to serve our
HTML through the public folder we created:
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 4/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
We also need to specify a port our app is going to run on. Let's go with 3000:
const port = process.env.PORT || 3000;
let io = socketIO(server);
app.use(express.static(public path));
Cool, we are all set. Now go inside the public folder, create a simple index.html file
and add something to it. I am going to create a blank HTML document and add an h3
tag containing "Welcome to the socket.io training!". Go to your browser and type
localhost:3000 to verify everything is working.
Perfect. Let's advance!
Setting up the front-end
As this is a socket.io tutorial I'm not going to fall into details on how to set-up your
HTML & CSS. Instead, I'm going to give you a link to the project without the socket.io
part and we will build that instead. link
If you clone the project, keep in mind you have to create the folder server and add
your server.js file. You also need to add app.js inside the public/js folder.
Socket.io
Variables
Okay, we already know what socket.io is and how it works. It is now time to make it
work with our buttons. Insite public/js we are going to create an app.js file. We add
our socket at the top
let socket = io();
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 6/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
We get our starting section. It contains components, visible before we click the
"Start" button and disappear when we start the game.
const startingSection = document.querySelector('.starting-section');
This is a built-in function. Whenever a new connection is made, the code within it will
be executed. We include another built-in function disconnect which is self-
explanatory.
We can test this by starting nodemon (nodemon server/server.js) and browsing to
localhost:3000. Open the terminal in VS Code and check if a message is being
logged.
Start Game real-time
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 7/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
Let's create the functionality for the "Start" button. We are going to hide the
.starting-section components and also make it real-time so they hide for other
players as well. Navigate to app.js and add a click event listener to the start button:
start button.addEventListener('click', () => {
})
Inside our event listener, we must emit a message to the socket.io server with
socket.emit('startGame');:
startButton.addEventListener('click', () => {
socket.emit('startGame');
});
Now on the server side(server.js), we must create a 'listener' for that message:
socket.on('startGame', () => {
})
socket.on('startGame', () => {
io.emit('startGame');
})
socket.on('startGame', () => {
});
socket.on('startGame', () => {
hideStartButton();
});
crazyButton.addEventListener('click', () => {
socket.emit('crazyIsClicked', {
offsetLeft: Math.random() * ((window.innerWidth - crazyButton
offsetTop: Math.random() * ((window.innerHeight - crazyButton
});
})
The server then receives these values under the form of 'data' variable & emits them
back:
socket.on('crazyIsClicked', (data) => {
io.emit('crazyIsClicked', data);
});
Our app.js file then receives the data and input it in the function, which moves the
button:
socket.on('crazyIsClicked', (data) => {
goCrazy(data.offsetLeft, data.offsetTop);
});
left = offLeft;
top = offTop;
And we test:
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 10/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
Latest Comments
Gianpierre Velasquez 1 month ago
Rookie
Level 1
Nice job, thanks for sharing!
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 11/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 12/13
20/11/2024, 23:15 How to use Socket.IO and build your first multiplayer game! - geeklore.io
https://geeklore.io/articles/how-to-use-socketio-and-build-your-first-multiplayer-game?ref=dailydev 13/13