Skip to content

Commit

Permalink
feat: switch to dragonfly db
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Jul 15, 2024
1 parent 4592cb7 commit 6148a59
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

- Node.js v20
- PNPM
- Docker
- Docker (dragonflydb - 3.5GB memory / 4 cores)

## Tech Stacks

- Frontend - React.js, TailwindCSS, React-Query
- Backend - Express, socket.io, PostgreSQL, Redis
- Backend - Express, socket.io, PostgreSQL, DragonflyDB

## Get Started

### Run final build using docker compose

- Spin up the entire stack (Redis, PostgreSQL, React.js, Node.js)
- Spin up the entire stack (DragonflyDB, PostgreSQL, React.js, Node.js)
```bash
pnpm docker:up
```
Expand All @@ -41,12 +41,12 @@ pnpm docker:down

### Development

- Spin up PostgreSQL and Redis
- Spin up PostgreSQL and DragonflyDB
```bash
docker compose up -d
```

> Make sure the ports (postgres: 5432, redis: 6379) are open for connection
> Make sure the ports (postgres: 5432, dragonfly: 6379) are open for connection
- Install dependencies
```bash
Expand Down Expand Up @@ -105,7 +105,7 @@ pnpm --filter web test
- [x] realtime messaging
- [x] typing indicators
- [x] socket.io cluster adapter integration
- [x] redis implementation (typing users, online users)
- [x] dragonflydb implementation (typing users, online users)
- [x] member online status
- [x] realtime member list update
- [x] infinite scroll cursor pagination (messages/groups/members)
Expand Down
11 changes: 7 additions & 4 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ services:
timeout: 10s
retries: 5

redis:
image: redis:7.2-alpine
restart: always
dragonfly:
image: 'docker.dragonflydb.io/dragonflydb/dragonfly'
restart: unless-stopped
command: dragonfly --proactor_threads=4
ports:
- '6379:6379'
- 6379:6379
ulimits:
memlock: -1
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 30s
Expand Down
9 changes: 6 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ services:
environment:
POSTGRES_PASSWORD: secret
POSTGRES_DB: mchat
redis:
image: redis:7.2-alpine
restart: always
dragonfly:
image: 'docker.dragonflydb.io/dragonflydb/dragonfly'
restart: unless-stopped
command: dragonfly --proactor_threads=4
ports:
- 6379:6379
ulimits:
memlock: -1
2 changes: 1 addition & 1 deletion server/.env.docker
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PORT=5000
JWT_SECRET=1009830912idhsjhdjfhiuh29hef98h1f8hf28hf98hef98h2f98fshdfhkjhajh
CORS_ORIGIN=http://localhost:3000
REDIS_HOST=redis
REDIS_HOST=dragonfly
REDIS_PORT=6379
DB_URL="postgresql://postgres:secret@db:5432/mchat"
3 changes: 2 additions & 1 deletion server/src/modules/members/members.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ export const getGroupMembers: RequestHandler = async (req, res, next) => {
const userIds = result.data.map(member => member.userId)

const onlineMembers = await checkOnlineUsers(userIds)
console.log('online_members', onlineMembers)

const membersWithOnlineStatus = result.data.map((member, index) => ({
...member,
online: onlineMembers[index] === 1,
online: onlineMembers[index] == 1,
}))

res.json({ data: membersWithOnlineStatus, cursor: result.cursor })
Expand Down
2 changes: 2 additions & 0 deletions server/src/redis/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export const markUserOffline = (userId: number) => {
}

export const checkOnlineUsers = async (userIds: number[]) => {
// redis: array of 1, 0
// dragonfly: array of '1', '0'
return redisClient.smismember(redisKeys.ONLINE_USERS, userIds)
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function getRedisClient() {
})

redisClient.on('connect', () => {
console.log('Redis connected'.yellow.bold)
console.log('DragonflyDB connected'.yellow.bold)
})

redisClient.on('error', (...args) => {
console.log('Redis error: '.red.bold, ...args)
console.log('DragonflyDB error: '.red.bold, ...args)
})
}

Expand Down

0 comments on commit 6148a59

Please sign in to comment.