This is a real-time communication server built with the Go Programming Language. It employs the Model-View-Controller-Service (MVCS) design pattern and a hub-based architecture to manage WebSocket connections and broadcast messages.
- A robust WebSocket server delivering seamless real-time communication between users
- Integrated PostgreSQL database with migrations for reliable data management
- Docker Compose for straightforward development environment setup
- A simple and intuitive API for user management
- A well-structured codebase that is easy to understand and extend
- A Makefile for common development tasks
- Documentation for all endpoints and actions
The project layout is structured as follows:
server
: This is the primary package for the Go WebSocket server code.cmd
: The main application entry pointdb
: This package contains everything related to database code and migrationsdockerfiles
: Contains Dockerfile for setting up your development environmentinternal
: This contains several internal packages that follow the MVCS designuser
: Dedicated to user managementws
: Manages WebSocket communication in line with the hub architecture
Makefile
: Useful for common development tasksrouter
: Houses the HTTP router and API routesutil
: A collection of utility functions
The MVCS pattern is chosen over traditional MVC due to the additional service layer it introduces. By adding a service layer, business logic and high-level operations are better managed. This separation of concerns improves scalability, reusability, testability, and flexibility. The implementation details include:
- Model: The
internal
package has data models and database-related code. - View: The
internal/ws
package handles WebSocket connections and messages. - Controller: The WebSocket handlers manage flow, routing, and interaction with models and the WebSocket hub.
- Service: Encapsulates complex business logic and service operations.
- Repository: Manages database operations and queries.
- Hub: Defined in
internal/ws/hub.go
, it manages WebSocket connections, client additions, removals, and message broadcasts. - Client: Each WebSocket client is represented by a "client" object defined in
internal/ws/client.go
. - WebSocket Handlers: Handlers in
internal/ws/ws_handler.go
interact with the hub to handle WebSocket messages. - Initialization and Management: Hub manages client connections and handles client additions and disconnections.
- Broadcasting: Efficient broadcasting of messages to all connected clients.
This project utilises a "users" table. The SQL code to create the table and associate indexes features below:
-- Create Users Table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);
-- Create Indexes
CREATE INDEX idx_username ON users (username);
CREATE INDEX idx_email ON users (email);
Ensure you have the following installed on your machine:
- Docker and Docker Compose
- Go is optional, only if you wish to build locally.
-
Clone this repository:
git clone https://github.com/Droxt1/WebSOCKET.git
-
Navigate into the project directory:
cd WebSOCKET
-
Use Docker Compose to start the development environment:
make dockerup
Instructions on creating and applying database migrations:
-
To create a new migration:
make createmigration
-
To apply pending migrations:
make migrateup
Ensure to put the SQL code in the
sql.up
file before applying migrations.