Skip to content

ricin9/chat-redis-pubsub

Repository files navigation

This is a modern, real-time chat application built with Go, leveraging the power of Fiber, HTMX, Tailwind CSS, and Templ. It uses SQLite for persistent storage and Redis for real-time functionality.

Features

  • Real-time room-based with admin role messaging
  • Persistent chat history with cursor pagination
  • Persistant unread message count for each room
  • Admin can add, kick, promote, demote a member
  • Online status for users
  • Responsive design with Tailwind CSS
  • Server-side rendered templates with Templ
  • Client-Server interactions using HTMX

Tech Stack

  • Go - Backend language
  • Fiber - Web framework
  • HTMX - Frontend interactivity
  • Templ - Type-safe Go HTML templating
  • Tailwind CSS - Styling
  • SQLite - Database
  • sql-migrate - Handling database migrations
  • Redis - In-memory data store used for its Pub/Sub capabilities
  • Air - Live reload for Go apps
  • Docker - Containerization
  • Fly.io - Deployment platform

Prerequisites

  • Go 1.20 or higher
  • Docker (for deployment)
  • Redis server

Environment Variables

The application uses two environment variables:

  • DATABASE_PATH: Path to the SQLite database file, Default : ./test.db
  • REDIS_CONN_STRING: Connection string for Redis, Default : uses local instance :6379

Development

  1. Clone the repository:
   git clone https://github.com/ricin9/chat-redis-pubsub
   cd chat-redis-pubsub
  1. Install dependencies:
   go mod tidy
   go install github.com/rubenv/sql-migrate/...@latest # sql-migrate cli
   go install github.com/air-verse/air@latest          # live reload
   go install github.com/a-h/templ/cmd/templ@latest    # template engine
  1. Setup redis

You can either install redis locally using your package manager or docker, or use remote connection string like below

export REDIS_CONN_STRING=redis://<user>:<password>@<host>:<port>
  1. Run the development server:

I have not yet made development scripts/makefiles yet so you are going to have to do this one by one.

# runs main.go with live reload
air
# html template generation
templ generate --watch ./views
# generates tailwindcss classes
npx tailwindcss -c ./views/tailwind.config.js -i ./views/input.css -o ./static/css/output.css --watch
  1. Open your browser and navigate to http://127.0.0.1:3000

Build

  1. Generate production templates and styles
templ generate ./views
npx tailwindcss -c ./views/tailwind.config.js -i ./views/input.css -o ./static/css/output.css --minify
  1. Build the Docker image
docker build -t chat-app .

Deployment

This project uses Docker and Fly.io for deployment.

  1. Install fly.io CLI if you don't already have it. Guide

  2. Since fly.io filesystem is ephemeral and we are using sqlite3 db, we need to create a volume.

fly volume create sqlite_db -r <region> -n 1

Region must be the same one you're deploying the app to.

  1. Repeat Step 1 from Build Steps

  2. Deploy to Fly.io:

flyctl deploy

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues

Open an issue if you found a bug, want to sugges changes, refactors, ...etc

About

Chat app made with golang, fiber, redis, and htmx

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published