import fp from 'fastify-plugin' import { type FastifyPluginAsync } from 'fastify' import { PgPubSub } from '@imqueue/pg-pubsub' import { env } from '../config/env' import logger from '../utils/logger' declare module 'fastify' { interface FastifyInstance { pubsub: PgPubSub } } const pubsubPlugin: FastifyPluginAsync = async (fastify) => { const pubsub = new PgPubSub({ singleListener: false, connectionString: env.DATABASE_URL, }) await pubsub.connect().catch(err => logger.error('PubSub error:', err)); fastify.decorate('pubsub', pubsub) // Graceful shutdown fastify.addHook('onClose', async () => { await pubsub.close() }) pubsub.on('connect', () => { logger.debug('✅ PubSub connected'); }); pubsub.on('end', () => logger.warn('pubsub Connection closed!')); } // Export plugin with metadata export default fp(pubsubPlugin, { fastify: '5.x', name: 'pubsub' })