Skip to content

Commit

Permalink
chore: switch to redis streams adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Jul 17, 2024
1 parent 107bf80 commit 619df55
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 70 deletions.
33 changes: 16 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"author": "Aseer KT",
"license": "ISC",
"dependencies": {
"@socket.io/cluster-adapter": "^0.2.2",
"@socket.io/sticky": "^1.0.4",
"@socket.io/redis-streams-adapter": "^0.2.2",
"argon2": "^0.40.3",
"colors": "^1.4.0",
"cors": "^2.8.5",
Expand Down
58 changes: 7 additions & 51 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import {
createAdapter as createClusterAdapter,
setupPrimary,
} from '@socket.io/cluster-adapter'
import { setupMaster, setupWorker } from '@socket.io/sticky'
import { createAdapter } from '@socket.io/redis-streams-adapter'
import 'colors'
import cors from 'cors'
import express from 'express'
import helmet from 'helmet'
import morgan from 'morgan'
import cluster from 'node:cluster'
import { createServer } from 'node:http'
import { availableParallelism } from 'node:os'
import { Server } from 'socket.io'
import swaggerUi from 'swagger-ui-express'
import { config } from './config'
import { connectDB } from './database'
import { errorHandler } from './middlewares'
import { getRedisClient } from './redis'
import rootRouter from './routes'
import { registerSocketEvents } from './socket/events'
import { socketAuthMiddleware } from './socket/middlewares'
Expand All @@ -28,40 +23,10 @@ import {
import swaggerDocument from './swagger-output.json'

const createApp = async () => {
if (cluster.isPrimary && config.isProd) {
console.log(`Primary ${process.pid} is running`)

const numCPUs = availableParallelism()

const httpServer = createServer()

// setup sticky sessions
setupMaster(httpServer, { loadBalancingMethod: 'least-connection' })

// setup connection between the workers
setupPrimary()

httpServer.listen(config.port, () => {
console.log(`Server running at http://localhost:${config.port}`.blue.bold)
})

for (let i = 0; i < numCPUs; i++) {
// Spawn a new worker process.
// This can only be called from the primary process.
cluster.fork()
}

cluster.on('exit', worker => {
console.log(`worker ${worker.process.pid} died`)
cluster.fork()
})

return
}

console.log(`Worker ${process.pid} started`)

await connectDB()
const redisClient = getRedisClient()

const app = express()

Expand All @@ -81,16 +46,9 @@ const createApp = async () => {
SocketData
>(server, {
cors: { origin: config.corsOrigin },
adapter: createAdapter(redisClient),
})

if (config.isProd) {
// use cluster adapter
io.adapter(createClusterAdapter())

// setup connection with primary process
setupWorker(io)
}

io.use(socketAuthMiddleware)

registerSocketEvents(io)
Expand All @@ -108,11 +66,9 @@ const createApp = async () => {

app.use(errorHandler)

if (!config.isProd) {
server.listen(config.port, () => {
console.log(`Server running at http://localhost:${config.port}`.blue.bold)
})
}
server.listen(config.port, () => {
console.log(`Server running at http://localhost:${config.port}`.blue.bold)
})

return { server }
}
Expand Down

0 comments on commit 619df55

Please sign in to comment.