gosocket

A zero-complexity pub/sub WebSocket server built for smaller projects and rapid prototyping. Spin up instantly, deploy with ease, and focus on your application logic, not infrastructure.

Simplicity First

gosocket is designed for developers who need to add real-time capabilities to smaller apps, prototypes, or MVPs in minutes. It avoids the overhead of complex message brokers by providing a single, focused binary that handles rooms and message broadcasting out of the box.

Installation

Install gosocket using Go:

go install github.com/awoldt/gosocket@latest

Or clone and build from source:

git clone https://github.com/awoldt/gosocket.git
cd gosocket
go build -o gosocket .

Features

Designed for developers who need a reliable WebSocket layer without the overhead of complex message brokers.

Quick Start

Start the WebSocket server:

gosocket start

On first run, a config.yaml file is automatically created with default settings. The server listens on port 8080 by default.

Configuration

Configure your server by editing config.yaml:

# config.yaml
allowed_origins: []        # Empty allows all origins
read_buffer_size: 1024
write_buffer_size: 1024
port: "8080"
auth_token: ""             # Set a token to require authentication
Option Description Default
allowed_origins List of allowed origins for CORS. Empty array allows all. []
read_buffer_size WebSocket read buffer size in bytes. 1024
write_buffer_size WebSocket write buffer size in bytes. 1024
port Port the server listens on. "8080"
auth_token Optional token for authentication. Empty disables auth. ""

Connecting to Rooms

Clients connect to rooms via the URL path. The path becomes the room name:

// Connect to the "chat" room
ws://localhost:8080/chat

// Connect to the "notifications" room
ws://localhost:8080/notifications

// Connect with authentication token
ws://localhost:8080/chat?token=your-secret-token

JavaScript Example

// Connect to a room
const ws = new WebSocket('ws://localhost:8080/chat');

ws.onopen = () => {
    console.log('Connected to chat room');
    ws.send('Hello, room!');
};

ws.onmessage = (event) => {
    console.log('Received:', event.data);
};

ws.onclose = () => {
    console.log('Disconnected');
};

gosocket uses a simple pub/sub model:

  1. Client connects to a WebSocket endpoint with a path (e.g., /chat)
  2. The path determines which "room" the client joins
  3. When a client sends a message, it's broadcast to all clients in that room
  4. When a client disconnects, they're removed from the room
  5. Empty rooms are automatically cleaned up

Authentication

To enable authentication, set the auth_token in your config:

auth_token: "my-secret-token"

Clients must then include the token as a query parameter:

ws://localhost:8080/chat?token=my-secret-token

Connections without a valid token will receive a 401 Unauthorized response.