A concise, simple, and robust WebSocket broadcast server implemented in Go using Gorilla WebSocket.
- Easy to Contribute: Very readable, idiomatic Go code, clear separation of logic, and helpful comments.
- Simple & Short: Everything in one small file ~150 lines. Easily fits into any Go project or can be copy-pasted.
- Minimal Setup: Zero external dependency management beyond Gorilla. Health check, configurable timeouts, and broadcasting out-of-the-box.
- Multiple client connections
- Broadcast messages to all connected clients
- Ping/Pong heartbeat mechanism
- Connection cleanup on client disconnect
- Configurable HTTP timeouts
- Built-in health check endpoint (
/health
)
go run .
By default, it starts on :3030
.
- URL:
ws://localhost:3030/ws
- Any message sent will be broadcast to all connected clients.
curl http://localhost:3030/health
# response: ok
Flag | Default | Description |
---|---|---|
-addr |
:3030 |
HTTP service address |
-readTimeout |
5s |
HTTP read timeout |
-writeTimeout |
5s |
HTTP write timeout |
-idleTimeout |
60s |
HTTP idle timeout |
Example:
go run main.go -addr=:8080 -readTimeout=10s
const socket = new WebSocket("ws://localhost:3030/ws");
socket.onopen = () => {
console.log("Connected");
socket.send("Hello from client!");
};
socket.onmessage = (event) => {
console.log("Received:", event.data);
};
socket.onclose = () => {
console.log("Disconnected");
};
- Go 1.23.5
- Gorilla WebSocket
go mod download