Skip to content

Bas3line/ws-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket Broadcast Server (Go)

Go WebSocket Gorilla

A concise, simple, and robust WebSocket broadcast server implemented in Go using Gorilla WebSocket.


Why use this WebSocket server?

  • 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.

📦 Features

  • 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)

Usage

Run the server

go run .

By default, it starts on :3030.

Connect via WebSocket

  • URL: ws://localhost:3030/ws
  • Any message sent will be broadcast to all connected clients.

Health check

curl http://localhost:3030/health
# response: ok

Configuration

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

Example (JavaScript)

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");
};

📦 Dependencies

Install deps:

go mod download

About

reliable ws service in go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages