Skip to content

Building a Real-Time Chat App in Go with Redis Pub/Sub and WebSockets

Notifications You must be signed in to change notification settings

thesaltree/redis-chat-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Redis Pub/Sub Chat Application

This is a simple real-time chat application built with Go, WebSockets, and Redis Pub/Sub. Users can join the chat by connecting to the WebSocket server and will receive messages from other users in real-time.

Features

  • Real-Time Communication: Messages are sent and received instantly using WebSockets.
  • Redis Pub/Sub: Used to broadcast messages to all connected users efficiently.
  • Graceful Shutdown: Ensures all connections are closed properly when the server shuts down.

Prerequisites

  • Go (version 1.16 or later)
  • Redis server (running on localhost:6379 by default)

Getting Started

1. Clone the Repository

git clone https://github.com/yourusername/redis-chat-app.git
cd redis-chat-app

2. Install Dependencies

This application uses the github.com/go-redis/redis/v8 and github.com/gorilla/websocket packages. You can install them with:

go get github.com/go-redis/redis/v8
go get github.com/gorilla/websocket

3. Configure Redis

Ensure your Redis server is running on localhost:6379. If it's hosted elsewhere, update the redisAddr constant in the code to reflect the correct address.

4. Run the Application

Start the server by running:

go run main.go

The server will start on http://localhost:8081.

5. Connect to the Chat

To join the chat, open multiple browser tabs (or WebSocket clients) and connect to:

ws://localhost:8081/chat/username

Replace username with a unique name for each user. Messages sent by one user will be received by all other connected users.

Reference Screenshots

Initiate chat

Initiate chat

User 1

user1

User 2

user2

User 3

user3

About

Building a Real-Time Chat App in Go with Redis Pub/Sub and WebSockets

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages