diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4002a74..0f1dacf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,12 +41,9 @@ importers: server: dependencies: - '@socket.io/cluster-adapter': + '@socket.io/redis-streams-adapter': specifier: ^0.2.2 version: 0.2.2(socket.io-adapter@2.5.5) - '@socket.io/sticky': - specifier: ^1.0.4 - version: 1.0.4 argon2: specifier: ^0.40.3 version: 0.40.3 @@ -1420,6 +1417,10 @@ packages: '@jridgewell/trace-mapping@0.3.9': resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==} + '@msgpack/msgpack@2.8.0': + resolution: {integrity: sha512-h9u4u/jiIRKbq25PM+zymTyW6bhTzELvOoUd+AvYriWOAKpLGnIamaET3pnHYoI5iYphAHBI4ayx0MehR+VVPQ==} + engines: {node: '>= 10'} + '@nodelib/fs.scandir@2.1.5': resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==} engines: {node: '>= 8'} @@ -1581,17 +1582,14 @@ packages: '@sinclair/typebox@0.27.8': resolution: {integrity: sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==} - '@socket.io/cluster-adapter@0.2.2': - resolution: {integrity: sha512-/tNcY6qQx0BOgjl4mFk3YxX6pjaPdEyeWhP88Ea9gTlISY4SfA7t8VxbryeAs5/9QgXzChlvSN/i37Gog3kWag==} - engines: {node: '>=10.0.0'} - peerDependencies: - socket.io-adapter: ^2.4.0 - '@socket.io/component-emitter@3.1.2': resolution: {integrity: sha512-9BCxFwvbGg/RsZK9tjXd8s4UcwR0MWeFQ1XEKIQVVvAGJyINdrqKMcTRyLoK8Rse1GjzLV9cwjWV1olXRWEXVA==} - '@socket.io/sticky@1.0.4': - resolution: {integrity: sha512-VuauT5CJLvzYtKIgouFSQ8rUaygseR+zRutnwh6ZA2QYcXx+8g52EoJ8V2SLxfo+Tfs3ELUDy08oEXxlWNrxaw==} + '@socket.io/redis-streams-adapter@0.2.2': + resolution: {integrity: sha512-BMPa6oGC0wFgpMXoGksbJ75zMBwk+79pxjHc2YusdoK+X0BxN4fTsqEBuFV7yeXi9ekbi87rwlsT61+WZGVW9g==} + engines: {node: '>=14.0.0'} + peerDependencies: + socket.io-adapter: ^2.5.4 '@surma/rollup-plugin-off-main-thread@2.2.3': resolution: {integrity: sha512-lR8q/9W7hZpMWweNiAKU7NQerBnzQQLvi8qnTDU/fxItPhtZVMbPV3lbCwjhIlNBe9Bbr5V+KHshvWmVSG9cxQ==} @@ -5998,6 +5996,8 @@ snapshots: '@jridgewell/sourcemap-codec': 1.4.15 optional: true + '@msgpack/msgpack@2.8.0': {} + '@nodelib/fs.scandir@2.1.5': dependencies: '@nodelib/fs.stat': 2.0.5 @@ -6120,17 +6120,16 @@ snapshots: '@sinclair/typebox@0.27.8': {} - '@socket.io/cluster-adapter@0.2.2(socket.io-adapter@2.5.5)': + '@socket.io/component-emitter@3.1.2': {} + + '@socket.io/redis-streams-adapter@0.2.2(socket.io-adapter@2.5.5)': dependencies: + '@msgpack/msgpack': 2.8.0 debug: 4.3.5 socket.io-adapter: 2.5.5 transitivePeerDependencies: - supports-color - '@socket.io/component-emitter@3.1.2': {} - - '@socket.io/sticky@1.0.4': {} - '@surma/rollup-plugin-off-main-thread@2.2.3': dependencies: ejs: 3.1.10 diff --git a/server/package.json b/server/package.json index 1ab0981..717b2d8 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/src/index.ts b/server/src/index.ts index c0f0cc3..ff948bb 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -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' @@ -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() @@ -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) @@ -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 } }