|
| 1 | +import { WebSocketGateway, WebSocketServer, SubscribeMessage, ConnectedSocket, MessageBody } from '@nestjs/websockets'; |
| 2 | +import { Server, Socket } from 'socket.io'; |
| 3 | +import { UseFilters, UseGuards } from '@nestjs/common'; |
| 4 | +import { OriginLogger } from '@multiversx/sdk-nestjs-common'; |
| 5 | + |
| 6 | +import { QueryPagination } from 'src/common/entities/query.pagination'; |
| 7 | +import { WsValidationPipe } from 'src/utils/ws-validation.pipe'; |
| 8 | +import { WebsocketExceptionsFilter } from 'src/utils/ws-exceptions.filter'; |
| 9 | +import { WsSubscriptionLimiterGuard } from 'src/utils/ws.subscription.limiter'; |
| 10 | +import { RoomKeyGenerator } from './room.key.generator'; |
| 11 | +import { EventsService } from 'src/endpoints/events/events.service'; |
| 12 | +import { EventsCustomSubscribePayload } from 'src/endpoints/events/entities/events.custom.subscribe'; |
| 13 | +import { EventsFilter } from 'src/endpoints/events/entities/events.filter'; |
| 14 | +import { Events } from 'src/endpoints/events/entities/events'; |
| 15 | + |
| 16 | + |
| 17 | +@UseFilters(WebsocketExceptionsFilter) |
| 18 | +@WebSocketGateway({ cors: { origin: '*' }, path: '/ws/subscription' }) |
| 19 | +export class EventsCustomGateway { |
| 20 | + private readonly logger = new OriginLogger(EventsCustomGateway.name); |
| 21 | + |
| 22 | + static keyPrefix = 'custom-events-'; |
| 23 | + |
| 24 | + @WebSocketServer() |
| 25 | + server!: Server; |
| 26 | + |
| 27 | + constructor( |
| 28 | + private readonly eventsService: EventsService, |
| 29 | + ) { } |
| 30 | + |
| 31 | + @UseGuards(WsSubscriptionLimiterGuard) |
| 32 | + @SubscribeMessage('subscribeCustomEvents') |
| 33 | + async handleCustomSubscription( |
| 34 | + @ConnectedSocket() client: Socket, |
| 35 | + @MessageBody(new WsValidationPipe()) payload: EventsCustomSubscribePayload |
| 36 | + ) { |
| 37 | + const filterIdentifier = RoomKeyGenerator.deterministicStringify(payload); |
| 38 | + const roomName = `${EventsCustomGateway.keyPrefix}${filterIdentifier}`; |
| 39 | + |
| 40 | + if (!client.rooms.has(roomName)) { |
| 41 | + await client.join(roomName); |
| 42 | + } |
| 43 | + |
| 44 | + return { status: 'success' }; |
| 45 | + } |
| 46 | + |
| 47 | + @SubscribeMessage('unsubscribeCustomEvents') |
| 48 | + async handleCustomUnsubscribe( |
| 49 | + @ConnectedSocket() client: Socket, |
| 50 | + @MessageBody(new WsValidationPipe()) payload: EventsCustomSubscribePayload |
| 51 | + ) { |
| 52 | + const filterIdentifier = RoomKeyGenerator.deterministicStringify(payload); |
| 53 | + const roomName = `${EventsCustomGateway.keyPrefix}${filterIdentifier}`; |
| 54 | + |
| 55 | + if (client.rooms.has(roomName)) { |
| 56 | + await client.leave(roomName); |
| 57 | + } |
| 58 | + |
| 59 | + return { status: 'unsubscribed' }; |
| 60 | + } |
| 61 | + |
| 62 | + async pushEventsForTimestampMs(timestampMs: number): Promise<void> { |
| 63 | + try { |
| 64 | + const allEvents = await this.eventsService.getEvents( |
| 65 | + new QueryPagination({ size: 10000 }), |
| 66 | + new EventsFilter({ before: timestampMs, after: timestampMs }), |
| 67 | + ); |
| 68 | + |
| 69 | + const eventsFilteredForBroadcast: Map<string, Events[]> = new Map(); |
| 70 | + |
| 71 | + for (const event of allEvents) { |
| 72 | + const roomKeys = RoomKeyGenerator.generate( |
| 73 | + EventsCustomGateway.keyPrefix, |
| 74 | + event, |
| 75 | + EventsCustomSubscribePayload, |
| 76 | + ); |
| 77 | + |
| 78 | + for (const roomKey of roomKeys) { |
| 79 | + if (this.server.sockets.adapter.rooms.has(roomKey)) { |
| 80 | + if (!eventsFilteredForBroadcast.has(roomKey)) { |
| 81 | + eventsFilteredForBroadcast.set(roomKey, []); |
| 82 | + } |
| 83 | + eventsFilteredForBroadcast.get(roomKey)!.push(event); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + for (const [roomName] of eventsFilteredForBroadcast) { |
| 89 | + this.server.to(roomName).emit("customEventUpdate", { |
| 90 | + events: eventsFilteredForBroadcast.get(roomName), |
| 91 | + timestampMs, |
| 92 | + }); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + this.logger.error(error); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments