Implement socket.io v4

This commit is contained in:
Mirco 2024-06-06 09:01:58 +02:00
parent 1cdacb9d80
commit 1105b072e2
5 changed files with 3180 additions and 1675 deletions

View file

@ -1 +1,2 @@
PORT=3002 PORT=3002
CORS_ORIGIN='meet.jit.si'

View file

@ -1,4 +1,4 @@
FROM node:16.17-slim FROM node:22-bookworm-slim
WORKDIR /excalidraw-backend WORKDIR /excalidraw-backend
@ -9,4 +9,4 @@ RUN npm run build
EXPOSE 80 EXPOSE 80
EXPOSE 9090 EXPOSE 9090
CMD ["npm", "start"] CMD ["npm", "start"]

4764
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -9,22 +9,22 @@
}, },
"private": true, "private": true,
"engines": { "engines": {
"node": ">=14.0.0", "node": ">=20.0.0",
"npm": ">=7.0.0" "npm": ">=10.0.0"
}, },
"dependencies": { "dependencies": {
"@types/debug": "4.1.5", "@types/debug": "4.1.10",
"@types/express": "4.17.11", "@types/express": "4.17.20",
"@types/node": "14.14.31", "@types/ms": "0.7.33",
"@types/socket.io": "2.1.4", "@types/node": "20.8.7",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"debug": "4.3.1", "debug": "4.3.4",
"dotenv": "^10.0.0", "dotenv": "^16.0.0",
"express": "4.17.1", "express": "^4.19.2",
"socket.io": "^2.5.0", "socket.io": "^4.7.2",
"socket.io-prometheus-metrics": "^1.0.6", "socket.io-prometheus-metrics": "^1.0.6",
"ts-node-dev": "^1.1.8", "ts-node-dev": "^2.0.0",
"typescript": "4.2.3" "typescript": "5.2.2"
}, },
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
@ -37,16 +37,15 @@
}, },
"devDependencies": { "devDependencies": {
"@jitsi/eslint-config": "^4.1.0", "@jitsi/eslint-config": "^4.1.0",
"@types/dotenv": "^8.2.0", "@typescript-eslint/eslint-plugin": "6.8.0",
"@typescript-eslint/eslint-plugin": "5.30.5", "@typescript-eslint/parser": "6.8.0",
"@typescript-eslint/parser": "5.30.4", "eslint": "^8.1.0",
"eslint": "8.1.0", "eslint-plugin-import": "2.28.1",
"eslint-plugin-import": "2.25.2", "eslint-plugin-jsdoc": "46.8.2",
"eslint-plugin-jsdoc": "37.0.3", "eslint-plugin-typescript-sort-keys": "^3.1.0"
"eslint-plugin-typescript-sort-keys": "^2.1.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"bufferutil": "^4.0.6", "bufferutil": "^4.0.6",
"utf-8-validate": "^5.0.9" "utf-8-validate": "^6.0.3"
} }
} }

View file

@ -2,8 +2,8 @@ import debug from 'debug';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import express from 'express'; import express from 'express';
import http from 'http'; import http from 'http';
import socketIO, { Socket } from 'socket.io'; import { Socket, Server } from 'socket.io';
import * as prometheus from 'socket.io-prometheus-metrics'; //import * as prometheus from 'socket.io-prometheus-metrics';
const serverDebug = debug('server'); const serverDebug = debug('server');
@ -28,25 +28,22 @@ server.listen(port, () => {
serverDebug(`listening on port: ${port}`); serverDebug(`listening on port: ${port}`);
}); });
const io = socketIO(server, { const corsOptions = {
handlePreflightRequest: (req, res) => { origin: process.env.CORS_ORIGIN,
const headers = { methods: [ 'GET', 'POST' ]
'Access-Control-Allow-Headers': 'Content-Type, Authorization', };
'Access-Control-Allow-Origin': req.header?.origin ?? 'https://meet.jit.si',
'Access-Control-Allow-Credentials': true
};
res.writeHead(200, headers); const io = new Server(server, {
res.end(); allowEIO3: true,
}, cors: corsOptions,
maxHttpBufferSize: 20e6, maxHttpBufferSize: 20e6,
pingTimeout: 60000 pingTimeout: 60000
}); });
// listens on host:9090/metrics // listens on host:9090/metrics
prometheus.metrics(io, { // prometheus.metrics(io, {
collectDefaultMetrics: true // collectDefaultMetrics: true
}); // });
io.on('connection', socket => { io.on('connection', socket => {
serverDebug(`connection established! ${socket.conn.request.url}`); serverDebug(`connection established! ${socket.conn.request.url}`);
@ -60,27 +57,27 @@ io.on('connection', socket => {
users.splice(users.indexOf(socket), 1); users.splice(users.indexOf(socket), 1);
}); });
const clients = Object.keys(io.sockets.adapter.rooms[roomID].sockets); const clients = Object.keys(io.sockets.adapter.rooms.get(roomID)?.keys() ?? new Set<string>());
if (clients.length > userLimit) { if (clients.length > userLimit) {
clients.forEach((clientKey: string) => { clients.forEach((clientKey: string) => {
const clientSocket = io.sockets.connected[clientKey]; const clientSocket = io.sockets.sockets.get(clientKey);
serverDebug(`${clientSocket} has left the ${roomID} room because the user limit was reached.`); if (clientSocket !== undefined) {
clientSocket.leave(roomID); serverDebug(`${clientSocket} has left the ${roomID} room because the user limit was reached.`);
clientSocket.leave(roomID);
}
}); });
return; return;
} }
if (io.sockets.adapter.rooms.get(roomID)?.size ?? 0 <= 1) {
if (io.sockets.adapter.rooms[roomID].length <= 1) {
io.to(`${socket.id}`).emit('first-in-room'); io.to(`${socket.id}`).emit('first-in-room');
} else { } else {
socket.broadcast.to(roomID).emit('new-user', socket.id); socket.broadcast.to(roomID).emit('new-user', socket.id);
} }
io.in(roomID).emit( io.in(roomID).emit(
'room-user-change', 'room-user-change', Array.from(io.sockets.adapter.rooms.get(roomID) ?? [])
Object.keys(io.sockets.adapter.rooms[roomID].sockets)
); );
}); });
@ -104,7 +101,7 @@ io.on('connection', socket => {
const rooms = io.sockets.adapter.rooms; const rooms = io.sockets.adapter.rooms;
for (const roomID of Object.keys(socket.rooms)) { for (const roomID of Object.keys(socket.rooms)) {
const clients = Object.keys(rooms[roomID].sockets).filter(id => id !== socket.id); const clients = Array.from(rooms.get(roomID) ?? []).filter(id => id !== socket.id);
if (roomID !== socket.id) { if (roomID !== socket.id) {
socket.to(roomID).emit('user has left', socket.id); socket.to(roomID).emit('user has left', socket.id);