Socket.io in action
In this demo, we will make a server that accepts a socket connection. When it receives a message from the client, it will log it to the console. Let's start by creating the server and then move on to the client:
- Create a folder called
server
in your root directory. - Open a terminal in the
server
folder and run the following command:npm init -y
This will set up an empty npm package in the folder.
- In the same terminal, run the following command:
npm i express socket.io
Here, we are installing the
express
dependency for creating our server and thesocket.io
library. - Create an
app.js
file in theserver
folder and add the following code:const PORT = 3000 const express = require("express"); const server = express() .listen(PORT, () => console.log('Listening on ${PORT}'));
This creates a minimal Express server that listens for requests on port
3000
. As we have already learned, this socket connection is established with...