|
1 | | -This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). |
| 1 | +# Introduction |
2 | 2 |
|
3 | | -## Getting Started |
| 3 | +Chat App is a real time chat application designed for performance and scalability. |
4 | 4 |
|
5 | | -First, run the development server: |
| 5 | +### Motivation |
6 | 6 |
|
7 | | -```bash |
8 | | -npm run dev |
9 | | -# or |
10 | | -yarn dev |
11 | | -# or |
12 | | -pnpm dev |
13 | | -# or |
14 | | -bun dev |
15 | | -``` |
| 7 | +The motivation behind this project was to learn distributed system design and build a scalable architecture that can handle a large number of users and messages. |
| 8 | +I wanted to explore new technologies like Go, WebSockets, and Apache Kafka, and develop a real-time data pipeline that supports machine learning (ML) training. |
16 | 9 |
|
17 | | -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
| 10 | +# Tech Stack |
18 | 11 |
|
19 | | -You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. |
| 12 | +| Component | Technology Used | |
| 13 | +|------------------|--------------------------| |
| 14 | +| **Frontend** | (React/Next.js, TypeScript) | |
| 15 | +| **Backend** | Go | |
| 16 | +| **Database** | PostgreSQL (relational) + Redis (NoSQL) + xxx (NoSQL) | |
| 17 | +| **Caching** | Redis | |
| 18 | +| **Real-Time** | WebSockets (gorilla/websocket for Go) | |
| 19 | +| **Message Queue** |Apache Kafka | |
| 20 | +| **Deployment** | Docker Compose | |
20 | 21 |
|
21 | | -This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. |
| 22 | +# Design |
22 | 23 |
|
23 | | -## Learn More |
| 24 | +### Authentication |
24 | 25 |
|
25 | | -To learn more about Next.js, take a look at the following resources: |
| 26 | +I chose to not use third-party authentication libraries. Instead, I designed a refresh token and JWT-based access token system: |
| 27 | +- The refresh token is securely stored in an httpOnly cookie and managed as a key in Redis for fast retrieval and easy revocation. |
| 28 | +- The access token is a JWT that includes the user ID as a claim. It is stored in memory and has a short lifespan for improved security. |
| 29 | +- Using authContext in React, I share the access token across all child components and centralize the token refresh logic. |
26 | 30 |
|
27 | | -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. |
28 | | -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. |
| 31 | +### Websockets |
29 | 32 |
|
30 | | -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! |
| 33 | +I used the gorilla/websocket library for Go to manage WebSocket connections: |
| 34 | +- Created a hub that manages all WebSocket connections and broadcasts messages. |
| 35 | +- Mapped user IDs to each WebSocket client to support direct messaging. |
| 36 | +- To scale horizontally, I plan to use Kafka consumers to broadcast messages across multiple WebSocket servers. |
31 | 37 |
|
32 | | -## Deploy on Vercel |
| 38 | +### Kafka |
33 | 39 |
|
34 | | -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. |
| 40 | +I chose Apache Kafka because of its high throughput and fault-tolerant design. It also supports multiple consumers, which is ideal for adding new features in the future. |
35 | 41 |
|
36 | | -Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. |
| 42 | +<!-- # Difficulties |
| 43 | +- Designing how to handle errors in Kafka |
| 44 | +- Designing how to metigate eventual consistency in xxx --> |
0 commit comments