DEV Community

Joy Biswas
Joy Biswas

Posted on

Real-Time Server Monitoring with Go and WebSockets

For a long time I wanted to create a project with WebSocket but wasn't getting a good idea. One day I was checking my VPS's resources from htop. Then suddenly an idea popped in my mind: why not use WebSocket and do that remotely in real-time?

The Idea

The concept is simple. Instead of SSHing into my server every time I want to check CPU usage, memory, or disk space, I could build a web dashboard that streams this information live. WebSockets make this perfect because they keep a persistent connection open, pushing updates to the browser without constant polling.

The Tech Stack

As usual, I'm gonna use Go. For such a simple project, I used Go's standard net/http library for the web server, github.com/gorilla/websocket for WebSocket connections, and github.com/shirou/gopsutil to fetch system metrics.

The beauty of this stack is how lightweight it is. No heavy frameworks, no complex setup. Just three dependencies and you're good to go.

Quick note: Yes, tools like Prometheus and Grafana already do this (and better). But where's the fun in that? This project is all about learning how these things work under the hood.

How It Works

The server does two main things. First, it serves a simple HTML page with CSS and JavaScript that connects to the WebSocket endpoint. Second, it collects system stats using gopsutil and pushes them through the WebSocket connection every second or so.

On the backend, gopsutil makes it dead simple to grab CPU percentages, memory usage, disk space, and even network stats. The gorilla/websocket package handles the upgrade from HTTP to WebSocket and manages the connection lifecycle.

The frontend is basic HTML, CSS and JavaScript. When the page loads, it establishes a WebSocket connection and listens for messages. Each message contains JSON data with the current system stats, which gets displayed in real-time on the page.

Why This Project Matters

This might seem like a toy project, but it taught me some valuable concepts. I learned how WebSockets maintain stateful connections, how to handle concurrent clients in Go, and how to gather system metrics programmatically.

Plus, it's actually useful. I can now check my server's health from any browser without needing SSH access. You could extend this to monitor multiple servers, add alerts when resources hit certain thresholds, or even create graphs showing resource usage over time.

Themes

The dashboard comes with five different visual styles to match your preference. Here's what each theme looks like:

image
image
image
image
image

I'm not very good at designing UI, so it needs to be fixed a lot. If you're into frontend, feel free to contribute and make it look better.

Getting Started

If you want to try it yourself, here's how:

  1. Clone the repository:
   git clone https://github.com/joybiswas007/res_mon.git
   cd res_mon
Enter fullscreen mode Exit fullscreen mode
  1. Install the dependencies:
   go mod tidy
Enter fullscreen mode Exit fullscreen mode
  1. Run the server:
   go run main.go
Enter fullscreen mode Exit fullscreen mode
  1. Open your browser and go to http://localhost:8080

The code is straightforward, so you can easily modify it to track different metrics or change how often updates are sent.

One More Thing...

Oh, and I took the liberty to write a Dockerfile and docker-compose.yaml file. Was it necessary? Absolutely not. Could you just run go run main.go and call it a day? Yes. Did I spend extra time containerizing a project that literally runs on a single command? Also yes.

Top comments (0)