Socket.IO Server with Node.JS

Socket.IO Server with Node.JS

Socket.IO Server with Node.js

Before you start it we have Part 1 here. Please read that as well for basic concepts of sockets and communication.


We will do the following in this guide:

  • Install and Set the Node.js application
  • Install required dependencies
  • Make a simple connecting server with Socket.IO

Node Installation:

To set up a server using Node.js, make sure that you have the latest version of Node.js installed on your computer. I will be using the Windows Operating system.

After completing the installation process, please verify the installation. Open the command terminal to verify and run the following command:

node -v

If you get the installed version from the command we are good to go. Otherwise, revisit the installation process.

console state on installation
Nodejs installation verification

NODE SERVER-SIDE SETUP:

Create a new directory for your project and navigate to it in your terminal or command prompt.

mkdir test-project
cd test-proect
npm init -y

Your console may look something like this:

console view after installation completed
Node project setup console image

This will set up the base for Node.js application inside your workspace with the name “test-project”.

Installing Dependencies

We will use 2 actually famous packages of node.js from npm library of packages.

  • Express
  • Socket.IO

To install these packages type the following commands in vscode terminal.

npm install express
npm install socket.io

Now the package.json file will be something like this.

Package.json File view
package.json

Let's Make a simple server

Create a new JavaScript file in your project directory called “server.js”.

Open server.js in visual code and add the following code to set up a basic Node.js server:

const express = require('express')
const app = express();
const http = require('http').createServer(app);

http.listen(3000, () => 
? console.log('Server listening on port 3000');
});        

This will create an Express app and start a Node.js server on port 3000.

Add the following code to server.js to set up Socket.io:

const io = require('socket.io')(http);

io.on('connection', (socket) => 
? console.log('a user connected');

? socket.on('disconnect', () => {
? ? console.log('user disconnected');
? });

});        

This will create a new Socket.io instance that listens for connections on the Node.js server. When a user connects, the server will log a message to the console.

Run the following command in your terminal to start the Node.js server:

node server.js

This will start the server on port 3000.

Let's create a custom message to receive from the client and do something about it.

Update the "io.on.........." section like this:

io.on('connection', (socket) => 
? ? console.log('a user connected');


? ? socket.on('message', (data) => {
? ? ? ? 
? ? ? //Client have sent some message please do something about it. ?
? ? ? ? console.log('Message from client', data);
? ? ? //sending response to client side
? ? ? ? socket.emit('message', {date: new Date().getTime(), data: data});
? ? ? 
? ? ? });


? ? socket.on('disconnect', () => {
? ? ? console.log('user disconnected');
? ? });
});        


Congressssss we have our server ready to listen to a client.

If you have something in mind about sockets and communication please visit my previous article here.

Eduardo S.

Freelancer Unity Game Developer

1 年

Thank you very much.

要查看或添加评论,请登录

Muhammad Afzaal的更多文章

社区洞察

其他会员也浏览了