Skip to content

Commit

Permalink
feat: msgpack
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfran committed Feb 21, 2025
1 parent f191a75 commit 5717b67
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/app/init-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fastifyCookie from '@fastify/cookie';
import cors from '@fastify/cors';
import fastifyIO from 'fastify-socket.io';
import { parse } from 'cookie';
import customParser from 'socket.io-msgpack-parser';

import {
fastifyTRPCPlugin,
Expand Down Expand Up @@ -64,6 +65,7 @@ fastify.register(fastifyIO as any, {
origin: process.env['FRONTEND_URL'],
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
},
parser: customParser,
});

fastify.register(async function (fastify) {
Expand Down
30 changes: 16 additions & 14 deletions apps/api/src/simulate-users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { io } from 'socket.io-client';
import { v4 as uuidv4 } from 'uuid';
import customParser from 'socket.io-msgpack-parser';

/*
Tested scenario:
Expand Down Expand Up @@ -64,6 +65,7 @@ function createClient(index: number) {

const ws = io(`ws://localhost:${PORT}`, {
extraHeaders: { Cookie: AUTH },
parser: customParser,
});

const close = (reason: string) => {
Expand Down Expand Up @@ -152,20 +154,6 @@ function createClient(index: number) {
},
]);

intervalTimeout = setInterval(() => {
let timeout = 300;

if (!init) {
timeout = 500;
}

const diff = Date.now() - lastMessage;

if (diff > timeout) {
close(`Client ${index} timeout ${diff}`);
}
}, 100);

ws.on('board', (response) => {
lastMessage = Date.now();
clientData[clientId].totalMessages++;
Expand Down Expand Up @@ -201,6 +189,20 @@ function createClient(index: number) {
}
});

intervalTimeout = setInterval(() => {
let timeout = 500;

if (!init) {
timeout = 1000;
}

const diff = Date.now() - lastMessage;

if (diff > timeout) {
close(`Client ${index} timeout ${diff}`);
}
}, 100);

ws.on('disconnect', () => {
close(`Client ${index} disconnected`);
});
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/app/modules/ws/services/ws.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Store } from '@ngrx/store';
import { wsOpen } from '../ws.actions';
import { optimize } from '@tapiz/board-commons';
import { Router } from '@angular/router';
import customParser from 'socket.io-msgpack-parser';
import { NotificationService } from '../../../shared/notification/notification.service';
import { ConfigService } from '../../../services/config.service';

Expand All @@ -24,6 +25,7 @@ export class WsService {
autoConnect: false,
withCredentials: true,
transports: ['websocket', 'webtransport'],
parser: customParser,
});
correlationId = v4();
#reconnect = new Subject<void>();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"sharp": "^0.33.5",
"socket.io": "^4.7.5",
"socket.io-client": "^4.7.5",
"socket.io-msgpack-parser": "^3.0.2",
"transformation-matrix": "^2.16.1",
"tslib": "^2.6.2",
"type-fest": "^4.18.2",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 5717b67

Please sign in to comment.