Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket Chat

A chat server built with Go and WebSockets. I made this to learn Go's concurrency patterns.

Live link: https://websocket-chat-production-8e24.up.railway.app

What it does

  • Real-time messaging between users
  • Shows typing indicators
  • Username validation and join/leave notifications
  • Caps at 50 users so the server doesn't crash
  • Simple web interface included
  • Messages disappear when you refresh (no database)

Running it

Clone and run:

git clone https://github.com/dax-side/websocket-chat.git
cd websocket-chat
go mod download
go run main.go

Or build first:

go build -o chat-server main.go
./chat-server

Open http://localhost:8080 in your browser. Try multiple tabs with different usernames.

How it works

The server creates a Hub that manages all connected users. When someone joins, the Hub spawns two goroutines for them: one reads their messages, one sends messages to them.

Browser ←→ Client ←→ Hub ←→ All Other Clients

Key parts:

  • Hub: Coordinates everything, handles joins/leaves/broadcasts
  • Client: Represents one connected user
  • Goroutines: Each user gets two background workers
  • Channels: How goroutines talk to each other
  • Mutex: Prevents race conditions when updating the user list

Go patterns you'll see

  • Goroutines for concurrent I/O
  • Channels for passing messages between goroutines
  • Select statements for multiplexing channels
  • Mutex for protecting shared data
  • WebSocket connection handling
  • JSON encoding/decoding

Built with

  • Go 1.22+
  • gorilla/websocket package
  • Plain HTML/CSS/JavaScript frontend

Settings

Change these in the code:

  • Max users: 50 (in NewHub())
  • Port: 8080 (in main())
  • Username length: 20 chars max

License

MIT - do whatever you want with it.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages