Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile.socket
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ RUN npm install -g --unsafe-perm prisma
RUN npx prisma generate

EXPOSE 4000
EXPOSE 4001

CMD [ "npm", "run", "socket" ]
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ services:
SOCKET_PORT: '${SOCKET_PORT}'
ports:
- '127.0.0.1:${SOCKET_PORT}:4000'
- '127.0.0.1:${SOCKET_METRICS_PORT}:4001'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update le README

networks:
- back

Expand Down
46 changes: 46 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"node-fetch": "^2.6.7",
"node-pushnotifications": "^2.0.3",
"nodemailer": "^6.8.0",
"prom-client": "^14.1.0",
"socket.io": "^4.5.1",
"webhook-discord": "^3.7.8"
},
Expand Down
5 changes: 4 additions & 1 deletion src/services/socket/listeners/chat.socket.listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { AuthenticatedSocket } from '@interfaces/auth.interface';

import { throwIfNotFunction, throwIfNotNumber, throwIfNotString } from '@utils/controller.utils';
import { Logger } from '@utils/logs.utils';
import { handleSocketRawError, getUsersSockets } from '@utils/socket.utils';
import { handleSocketRawError } from '@utils/socket.utils';

import * as chatSocket from '../emitters/chat.socket.emitters';
import { totalUsersOnSocket } from '@services/socket/metrics/metrics.socket';

export function startSocket() {
chatNamespace.on('connection', async (socket: AuthenticatedSocket) => {
Expand All @@ -16,6 +17,7 @@ export function startSocket() {
handleSocketRawError(null, error);
}
const logger = new Logger('Chat Socket', socket.user);
totalUsersOnSocket.inc();
logger.log('Connected');

socket.on('join_conversation', async (data, callback) => {
Expand Down Expand Up @@ -106,6 +108,7 @@ export function startSocket() {
socket.on('disconnecting', async () => {
try {
await chatSocket.sendConnectionStatus(socket, false);
totalUsersOnSocket.dec();
logger.log('Disconnected');
} catch (error) {
console.error('Disconnection handling error', error);
Expand Down
1 change: 0 additions & 1 deletion src/services/socket/listeners/dropy.socket.listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export function startSocket() {
dropyNamespace.on('connection', async (socket: AuthenticatedSocket) => {
const logger = new Logger('Dropy Socket', socket.user);
socket.user = await resetUserBadgeNotification(socket.user);

logger.log('Connected');

socket.on('zones_update', async (data: any, callback) => {
Expand Down
14 changes: 14 additions & 0 deletions src/services/socket/metrics/metrics.socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Prometheus, { Counter, Gauge } from 'prom-client';
import { Express } from 'express';

export const totalUsersOnSocket = new Gauge({
name: 'total_users_on_socket',
help: 'Number of users connected to the socket',
});

export const addMetricsRoute = (server: Express) => {
server.get('/metrics', async (req, res) => {
res.set('Content-Type', Prometheus.register.contentType);
res.end(await Prometheus.register.metrics());
});
};
11 changes: 10 additions & 1 deletion src/services/socket/socket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Server } from 'socket.io';
import { NextFunction } from 'express';
import express, { NextFunction } from 'express';
import { addMetricsRoute } from './metrics/metrics.socket';

import { logStartedService } from '@/utils/logs.utils';

Expand All @@ -11,6 +12,7 @@ import * as dropySocket from './listeners/dropy.socket.listeners';
import * as chatSocket from './listeners/chat.socket.listeners';

const socketPort = 4000;
const metricsPort = 4001;

const io = new Server(socketPort, {
maxHttpBufferSize: 1e8,
Expand Down Expand Up @@ -38,4 +40,11 @@ chatSocket.startSocket();

logStartedService('Dropy Socket', socketPort);

const metrics_server = express();
addMetricsRoute(metrics_server);

metrics_server.listen(metricsPort, () => {
logStartedService('Socket Metrics', metricsPort);
});

export default io;