Skip to content

Instantly share code, notes, and snippets.

@bewithdhanu
Last active June 12, 2023 08:44
Show Gist options
  • Save bewithdhanu/483cc18ad46c0b02e1721a32a8ffcc1b to your computer and use it in GitHub Desktop.
Save bewithdhanu/483cc18ad46c0b02e1721a32a8ffcc1b to your computer and use it in GitHub Desktop.
Setting up a Node.js Socket Server on Ubuntu

Setting up a Node.js Socket Server

This guide will show you how to set up a Node.js Socket Server on a Linux machine. The server will use the Socket.io library to handle real-time communication between clients.

Prerequisites

Before we begin, ensure that you have the following:

  • A Linux machine with root access
  • Node.js and NPM installed on your machine

Installation

First, we need to install the necessary dependencies. Open a terminal and run the following commands:

sudo apt install nodejs
sudo apt install npm
npm install express
npm install socket.io
sudo npm i -g pm2

These commands will install Node.js, NPM, Express, Socket.io, and PM2.

Starting the Server

Next, we need to start the server. To do this, we will use PM2, a process manager for Node.js applications.

pm2 start server.js

This command will start the server and keep it running in the background. You can view the server logs with the following command:

pm2 log --lines 500

This will show the last 500 lines of server logs.

To check the status of the server, run:

pm2 status server.js

Setting up Automatic Startup

To ensure that the server starts automatically on system boot, we will use the PM2 startup command.

pm2 startup

This will generate a command that you need to run with root privileges. Copy the command and run it in the terminal. The generated command will look like this:

Generate an init script with pm2 startup. Run the command that is output by this command (it will be something like 
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u user --hp /home/user).

Replace user with the username of the user that will be running the Node.js Socket Server. This command will create a systemd unit that will automatically start the server on boot.

Finally, we need to save the current PM2 process list so that it can be loaded on startup.

pm2 save

This completes the setup of your Node.js Socket Server. You can now connect to the server from your client applications using Socket.io.

Stop PM2 for a Specific Application

To stop PM2 for a specific application, you can use the pm2 stop command followed by the name of the application. For example, to stop an application named server.js, you can use:

pm2 stop server.js

This will stop the server.js application.

Restart PM2 for a Specific Application

To restart PM2 for a specific application, you can use the pm2 restart command followed by the name of the application. For example, to restart an application named server.js, you can use:

pm2 restart server.js

This will stop and then start the server.js application.

Unstartup PM2 from Systemd

To unstartup PM2 from systemd, you can use the pm2 unstartup command followed by the init system you want to unstartup from. For example, to unstartup PM2 from systemd, you can use:

pm2 unstartup systemd

This will remove the PM2 systemd unit file, which will prevent PM2 from starting automatically on system boot.

Check if a Service is Running on Port 3000

To check if a service is running on port 3000, you can use the lsof command with the -i flag:

sudo lsof -i :3000

This will list all processes running on port 3000.

Kill a Process

To kill a process, you can use the kill command with the -9 flag followed by the process ID (PID):

sudo kill -9 <PID>

Replace <PID> with the process ID of the process you want to kill.

Note that it is important to use the -9 flag, as this sends a SIGKILL signal to the process, which forcefully terminates it.

Some other useful commands

Node.js Commands

  • npm init: Initializes a new Node.js project and creates a package.json file.
  • npm install <package-name>: Installs a Node.js package locally in the current project.
  • npm install <package-name> -g: Installs a Node.js package globally on your system.
  • npm update: Updates all packages listed in the package.json file to their latest versions.
  • npm start: Runs the start script specified in the package.json file.

PM2 Commands

  • pm2 list: Lists all running processes managed by PM2.
  • pm2 stop <app-name>: Stops a specific PM2-managed process.
  • pm2 restart <app-name>: Restarts a specific PM2-managed process.
  • pm2 delete <app-name>: Deletes a specific PM2-managed process.
  • pm2 monit: Launches a real-time monitoring dashboard for all PM2-managed processes.
  • pm2 log <app-name>: Shows the logs for a specific PM2-managed process.
  • pm2 save: Saves the current list of PM2-managed processes, so they can be automatically started on system boot.

These commands can be used to manage your Node.js and PM2 applications, and to monitor and troubleshoot any issues that may arise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment