From ef55d9bb24430610b60c1b77821307cd72c08c55 Mon Sep 17 00:00:00 2001 From: zwlucas Date: Sat, 25 Jul 2026 18:23:29 -0300 Subject: feat: upload of the project Signed-off-by: zwlucas --- api/src/plugins/events.plugin.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 api/src/plugins/events.plugin.ts (limited to 'api/src/plugins/events.plugin.ts') diff --git a/api/src/plugins/events.plugin.ts b/api/src/plugins/events.plugin.ts new file mode 100644 index 0000000..fdf0e74 --- /dev/null +++ b/api/src/plugins/events.plugin.ts @@ -0,0 +1,34 @@ +/** + * events.plugin.ts + * + * Expõe um EventEmitter global no fastify para pub/sub em memória. + * Ideal para disparar Server-Sent Events (SSE) aos clientes conectados. + */ + +import fp from 'fastify-plugin'; +import { EventEmitter } from 'node:events'; +import type { FastifyInstance } from 'fastify'; + +export interface AppEventMap { + 'ticket.created': { id: number; title: string; creatorId: number }; + 'ticket.updated': { id: number; status: string; assigneeId: number | null; creatorId: number; authorId: number }; + 'comment.added': { ticketId: number; authorId: number; isInternal: boolean; creatorId: number; assigneeId: number | null }; +} + +declare module 'fastify' { + interface FastifyInstance { + events: EventEmitter; + } +} + +async function eventsPlugin(fastify: FastifyInstance): Promise { + const emitter = new EventEmitter(); + // Permite muitos clientes SSE conectados simultaneamente + emitter.setMaxListeners(100); + + fastify.decorate('events', emitter); +} + +export default fp(eventsPlugin, { + name: 'events-plugin', +}); -- cgit v1.2.3