- NodeJS
16.20.0
- NPM
8.19.4
- ExpressJS
4.17.1
- NodeJS WebSocket Library
8.2.3
- PostgreSQL
12.14
-
Clone project repository :
$ git clone https://github.com/krlan2789/express-ws-app.git
-
Switch to the project directory :
$ cd express-wss-app
-
Install dependencies :
$ npm install or $ npm i
-
Duplicate the
.env_example
or create new file, then rename it to.env
. -
Change the value as needed :
DB_HOST=localhost # database hostname or ip DB_PORT=5432 # postgresql port DB_NAME=postgres # database name DB_USERNAME=root # database username DB_PASSWORD=12345678 # database password CONNECTION_PORT=8765 # node.js app port
-
Switch to the project directory :
$ cd express-wss-app
-
Start on development mode :
$ npm run dev
-
Start on production mode :
$ npm start
-
Start on production mode using pm2 (required pm2 package) :
$ npm start:pm2
-
Open browser, use websocket tester tools, then fill in the url with
[ws/wss]://[ip/hostname]:[port]?token=user_token
.Example : HTTP -> ws://localhost:8765?token=user1240635408404344554 HTTPS -> wss://localhost:8765?token=user1240635408404344554
Or, run
npm run test:ws
on terminal. -
Visit this article to run the app on VPS/Cloud hosting/etc.