Skip to main content
This guide walks you through building a real-time notification system. Users receive instant notifications via WebSocket.

Prerequisites

  • Go 1.22 or later
  • Basic understanding of Go and JavaScript

Step 1: Create Your Project

Step 2: Create the Server

Create main.go:

Step 3: Create the Frontend

Create public/index.html:

Step 4: Run and Test

Open http://localhost:8080 in your browser.
  1. Enter a user ID (e.g., β€œalice”) and click Connect
  2. Open another tab, connect as β€œbob”
  3. Send a notification from Alice to Bob
  4. Try the broadcast feature

Understanding the Code

Authentication

OnAuth returns:
  • (value, nil) - Accept connection, store value in session
  • (nil, error) - Reject connection

Message Handling

Messages from clients are JSON with topic and data fields:

Subscribe and Publish

Next Steps