support socket io redis adapter

This commit is contained in:
SrGooglo 2023-06-23 21:27:16 +00:00
parent 6be0dffebe
commit e7d7737281
16 changed files with 77 additions and 16 deletions

View File

@ -18,29 +18,33 @@
},
"license": "MIT",
"dependencies": {
"connect-mongo": "^4.6.0",
"minio": "^7.0.32",
"mongoose": "^6.9.0",
"redis": "^4.6.6",
"@foxify/events": "^2.1.0",
"@socket.io/cluster-adapter": "^0.2.2",
"@socket.io/sticky": "^1.0.3",
"@socket.io/redis-adapter": "^8.2.1",
"@socket.io/redis-emitter": "^5.1.0",
"axios": "^1.4.0",
"bcrypt": "5.0.1",
"comty.js": "^0.47.4",
"linebridge": "0.15.12",
"connect-mongo": "^4.6.0",
"corenode": "0.28.26",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"jsonwebtoken": "8.5.1",
"linebridge": "0.15.12",
"luxon": "^3.0.4",
"minio": "^7.0.32",
"moment": "2.29.4",
"moment-timezone": "0.5.37",
"mongoose": "^6.9.0",
"morgan": "^1.10.0",
"nanoid": "3.2.0",
"redis": "^4.6.6",
"socket.io": "^4.5.4"
},
"devDependencies": {
"cross-env": "^7.0.3",
"nodemon": "^2.0.15"
}
}
}

View File

@ -6,6 +6,7 @@ import http from "http"
import EventEmitter from "@foxify/events"
import ComtyClient from "@shared-classes/ComtyClient"
import RedisClient from "@shared-classes/RedisClient"
import routes from "./routes"
@ -25,6 +26,10 @@ export default class Server {
}
}
redis = global.redis = RedisClient({
withWsAdapter: true
})
comty = global.comty = ComtyClient()
eventBus = global.eventBus = new EventEmitter()

View File

@ -210,6 +210,10 @@ export default class ChatServer {
}
})
if (global.ioAdapter) {
this.io.adapter(global.ioAdapter)
}
this.RoomsController = new RoomsController(this.io)
}

View File

@ -17,28 +17,32 @@
"license": "MIT",
"dependencies": {
"@foxify/events": "^2.1.0",
"@socket.io/redis-adapter": "^8.2.1",
"@socket.io/redis-emitter": "^5.1.0",
"@socket.io/cluster-adapter": "^0.2.2",
"@socket.io/sticky": "^1.0.3",
"axios": "^1.4.0",
"bcrypt": "5.0.1",
"comty.js": "^0.47.4",
"connect-mongo": "^4.6.0",
"corenode": "0.28.26",
"linebridge": "0.15.12",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"connect-mongo": "^4.6.0",
"minio": "^7.0.32",
"mongoose": "^6.9.0",
"redis": "^4.6.6",
"express": "^4.18.2",
"jsonwebtoken": "8.5.1",
"linebridge": "0.15.12",
"luxon": "^3.0.4",
"minio": "^7.0.32",
"moment": "2.29.4",
"moment-timezone": "0.5.37",
"mongoose": "^6.9.0",
"morgan": "^1.10.0",
"nanoid": "3.2.0",
"redis": "^4.6.6",
"socket.io": "^4.5.4"
},
"devDependencies": {
"cross-env": "^7.0.3",
"nodemon": "^2.0.15"
}
}
}

View File

@ -38,7 +38,9 @@ export default class Server {
db = new DbManager()
redis = global.redis = RedisClient()
redis = global.redis = RedisClient({
withWsAdapter: true
})
storage = global.storage = StorageClient()

View File

@ -1,4 +1,6 @@
import socketio from "socket.io"
import { createAdapter } from "@socket.io/cluster-adapter"
import { setupWorker } from "@socket.io/sticky"
import withWsAuth from "@middlewares/withWsAuth"
@ -488,6 +490,10 @@ export default class RoomsServer {
}
})
if (global.ioAdapter) {
this.io.adapter(global.ioAdapter)
}
this.RoomsController = new RoomsController(this.io)
}

View File

@ -7,12 +7,25 @@
"dev": "cross-env NODE_ENV=development nodemon --ignore dist/ --exec corenode-node ./src/index.js",
"run:prod": "cross-env NODE_ENV=production node ./dist/index.js"
},
"sharedClasses": {
"FileUpload": "src/shared-classes",
"CacheService": "src/shared-classes",
"ComtyClient": "src/shared-classes",
"RedisClient": "src/shared-classes",
"StorageClient": "src/shared-classes",
"DbManager": "src/shared-classes",
"Errors": "src/shared-classes"
},
"license": "MIT",
"dependencies": {
"@corenode/utils": "0.28.26",
"@foxify/events": "^2.1.0",
"@jimp/plugin-scale": "^0.22.7",
"@octokit/rest": "^19.0.7",
"@socket.io/redis-adapter": "^8.2.1",
"@socket.io/redis-emitter": "^5.1.0",
"@socket.io/cluster-adapter": "^0.2.2",
"@socket.io/sticky": "^1.0.3",
"@tensorflow/tfjs-node": "4.0.0",
"axios": "^1.2.5",
"bcrypt": "^5.1.0",

View File

@ -12,7 +12,8 @@ import { User, Session, Config } from "@models"
import DbManager from "@classes/DbManager"
import { createStorageClientInstance } from "@classes/StorageClient"
import CacheService from "@classes/CacheService"
import RedisClient from "@shared-classes/RedisClient"
import internalEvents from "./events"
@ -42,7 +43,9 @@ export default class API {
},
)
cacheService = global.cacheService = new CacheService()
redis = global.redis = RedisClient({
withWsAdapter: true
})
DB = new DbManager()
@ -114,6 +117,7 @@ export default class API {
events = internalEvents
async initialize() {
await this.redis.initialize()
await this.DB.initialize()
await this.initializeConfigDB()
@ -222,6 +226,10 @@ export default class API {
})
}
if (global.ioAdapter) {
this.server.websocket_instance.io.adapter(global.ioAdapter)
}
this.server.websocket_instance.eventsChannels.push(["/main", "ping", async (socket) => {
return socket.emit("pong")
}])

View File

@ -0,0 +1 @@
/opt/comty-federated/@public/shared/classes/CacheService

View File

@ -0,0 +1 @@
/opt/comty-federated/@public/shared/classes/ComtyClient

View File

@ -0,0 +1 @@
/opt/comty-federated/@public/shared/classes/DbManager

View File

@ -0,0 +1 @@
/opt/comty-federated/@public/shared/classes/Errors

View File

@ -0,0 +1 @@
/opt/comty-federated/@public/shared/classes/FileUpload

View File

@ -0,0 +1 @@
/opt/comty-federated/@public/shared/classes/RedisClient

View File

@ -0,0 +1 @@
/opt/comty-federated/@public/shared/classes/StorageClient

View File

@ -1,4 +1,5 @@
import { createClient } from "redis"
import { createAdapter } from "@socket.io/redis-adapter"
function composeURL() {
// support for auth
@ -17,13 +18,20 @@ function composeURL() {
return url
}
export default () => {
export default ({
withWsAdapter = false
} = {}) => {
let client = createClient({
url: composeURL(),
password: process.env.REDIS_PASSWORD,
username: process.env.REDIS_USERNAME,
})
if (withWsAdapter) {
client.subClient = client.duplicate()
client.ioAdapter = global.ioAdapter = createAdapter(client, client.subClient)
}
client.initialize = async () => {
console.log("🔌 Connecting to Redis client...")