NodeJS WebSocket Server Implementation
Install project dependencies with node/npm
# Install all node/npm libraries, including dev-dependencies
npm install
# Install only those npm libraries needed to run the app in production
npm install --production
To attempt to install latest compatible packages version ~ Be sure to test app is functional afterwards, if not revert package-lock.json file, and run npm install
npm update
Now you can run the WebSocket server
npm start
If running on your local machine, you can access the demo-test page at http://localhost:8080/status.html?host=localhost:8080 *to use a custom host, just change the value of the host query
~The production demo-test page is at http://173.255.226.41:8080/status.html
To view http access logs, go to /access_log To view websocket access logs, go to /websocket_log
Connect/Disconnect from the WebSocket Server via the Connect and Close buttons. The present connection status or errors are show at top of page.
Select the context (control) of the message to send to the WebSocket-Server
- SEND_DATA: Send over the value in the text area, and get a mirrored response from the server
- RESET_SERVER: Tell the server to shutdown, then restart in ~5 seconds
- BROADCAST: Send over the value in the text area, and tell the server to broadcast that message to all connected clients
- TARGET_CLIENT: Send over the value in the text area, and tell the server to relay the message to a specific client
- *INIT_CLIENT: Internal control message used for handshaking with Web-Socket server;
Client sends over info to identify itself and its initial status (i.e. VAC3271, OK )
Server saves this info to the tracked client for future use. Server sends over list of all presently connected clients.
Trigger sending message to to the WebSocket Server via Send button.
View all presently connected clients to server in Blue table.
View messages received from Server in Bisque rectangle at page bottom.
On a linux machine that supports systemd services;
- place the websocket-server.service file in the /etc/systemd/system directory
- enable, and check that the service is running by
# enable the service
sudo systemctl enable websocket-server.service
# start the service
sudo systemctl start websocket-server
# check the service is enabled and running
systemctl status websocket-server
# check that nodejs program of service is running off port 8080
sudo lsof -i -P
The service is configured to start the WebSocket server on system startup, and to restart it on error.
In order to shutdown the service;
# stop the service
sudo systemctl stop websocket-server
# to disable the service from loading on reboot/startup
sudo systemctl disable websocket-server.service