How to Install and Use websocketd for Real‑Time WebSocket Scripts
This guide explains how to install the websocketd daemon, configure it to run shell scripts as WebSocket back‑ends, create simple HTML clients, monitor system memory in real time, and fine‑tune parameters such as static file serving, developer console, SSL, and Nginx proxying.
Installation
// Download
wget https://github.com/joewalnes/websocketd/releases/download/v0.4.1/websocketd-0.4.1-linux_amd64.zip
// Unzip
unzip websocketd-0.4.1-linux_amd64.zipAfter extraction the executable websocketd is available.
Check the version: ./websocketd -version Output example: websocketd 0.4.1 (go1.15.7 linux-amd64)
Simple Example – Counter Script
Shell script (counter.sh)
#!/bin/bash
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
echo $COUNT
sleep 1
doneStart websocketd
chmod 777 counter.sh
/var/www/websocketd/websocketd --port=8888 /var/www/websocketd/counter.shThe command launches a WebSocket server on port 8888. Each client connection triggers counter.sh, and its output is streamed to the client.
HTML client
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>websocketd client test</title>
</head>
<body>
<h2>websocketd client test</h2>
<pre id="message" style="font-size: 24px;"></pre>
<script>
function log(msg){
document.getElementById('message').textContent += msg + '
';
}
var ws = new WebSocket('ws://127.0.0.1:8888/');
ws.onopen = function(){ console.log('CONNECT'); };
ws.onclose = function(){ console.log('DISCONNECT'); ws.close(); };
ws.onmessage = function(event){
console.log('MESSAGE: ' + event.data);
log('websocketd server response: ' + event.data);
};
</script>
</body>
</html>Real‑Time Memory Monitoring
Shell script (system_info.sh)
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
SHELL_DIR="/var/www/websocketd"
SHELL_NAME="system_info"
SHELL_TIME=$(date '+%Y-%m-%d')
SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}-${SHELL_TIME}.log"
while true; do
FIND_DATA=$(cat /proc/meminfo | grep "MemFree:" | awk '{print $2}')
echo "[${SHELL_TIME}]: FIND_DATA = ${FIND_DATA}" >> $SHELL_LOG
echo '{"data":"'${FIND_DATA}'","errcode":0,"errmsg":0}'
sleep 2
doneStart websocketd for monitoring
chmod 777 system_info.sh
/var/www/websocketd/websocketd --port=8888 /var/www/websocketd/system_info.shParameter Configuration
Static file serving
Use --staticdir= to let websocketd serve static files such as counter.html or count.sh.
Example count script and HTML
#!/bin/bash
for COUNT in $(seq 1 10); do
echo $COUNT
sleep 1
done <!DOCTYPE html>
<html>
<head>
<title>websocketd count example</title>
<style>#count {font: bold 150px arial; margin:auto; padding:10px; text-align:center;}</style>
</head>
<body>
<div id="count"></div>
<script>
var ws = new WebSocket('ws://127.0.0.1:8888/');
ws.onopen = function(){ document.body.style.backgroundColor = '#cfc'; };
ws.onclose = function(){ document.body.style.backgroundColor = null; };
ws.onmessage = function(event){
document.getElementById('count').textContent = event.data;
};
</script>
</body>
</html>Run with:
/var/www/websocketd/websocketd --port=8888 --staticdir=. ./count.shDeveloper Console
Enable an interactive console without writing JavaScript by adding the --devconsole flag:
/var/www/websocketd/websocketd --port=8888 --dir=/var/www/websocketd --devconsoleRunning as a Daemon
nohup /var/www/websocketd/websocketd --port=8888 /var/www/websocketd/system_info.sh > /dev/null 2>&1 &SSL Certificate Loading
sudo websocketd \
--port=8888 \
--ssl \
--sslcert="/etc/letsencrypt/live/www.tinywan.com/fullchain.pem" \
--sslkey="/etc/letsencrypt/live/www.tinywan.com/privkey.pem" \
/var/www/websocketd/system.shNginx Proxy Configuration
server {
listen 443 ssl http2;
server_name wallet.tinywan.com;
ssl_certificate /etc/letsencrypt/wallet.tinywan.com/full_chain.pem;
ssl_certificate_key /etc/letsencrypt/wallet.tinywan.com/private.key;
location /wssd {
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 600;
}
}Clients can connect via:
var ws = new WebSocket('wss://wallet.tinywan.com/wssd');Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
