feat: WhatsApp Nucleo con Nuxt 4 + Baileys v7
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 6m46s
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 6m46s
Reemplazo completo de Evolution API por implementación directa con Baileys. Características: - Dashboard completo con Nuxt UI v4 - Soporte para múltiples instancias de WhatsApp - Conexión via QR code o pairing code - Persistencia de mensajes en PostgreSQL - API REST para integraciones externas - Webhooks con firma HMAC - SSE para actualizaciones en tiempo real - Autenticación con Authentik
This commit is contained in:
66
server/api/events/stream.get.ts
Normal file
66
server/api/events/stream.get.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* GET /api/events/stream
|
||||
* Server-Sent Events endpoint for real-time updates
|
||||
*/
|
||||
import { baileysManager } from '../../services/baileys/manager'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
// Set SSE headers
|
||||
setHeader(event, 'Content-Type', 'text/event-stream')
|
||||
setHeader(event, 'Cache-Control', 'no-cache')
|
||||
setHeader(event, 'Connection', 'keep-alive')
|
||||
|
||||
// Get the raw response
|
||||
const res = event.node.res
|
||||
|
||||
// Send initial connection message
|
||||
res.write(`data: ${JSON.stringify({ type: 'connected', timestamp: Date.now() })}\n\n`)
|
||||
|
||||
// Event handlers
|
||||
const handlers = {
|
||||
'instance.status': (data: any) => {
|
||||
res.write(`event: instance.status\ndata: ${JSON.stringify(data)}\n\n`)
|
||||
},
|
||||
'instance.qr': (data: any) => {
|
||||
res.write(`event: instance.qr\ndata: ${JSON.stringify(data)}\n\n`)
|
||||
},
|
||||
'instance.pairing': (data: any) => {
|
||||
res.write(`event: instance.pairing\ndata: ${JSON.stringify(data)}\n\n`)
|
||||
},
|
||||
'message.received': (data: any) => {
|
||||
res.write(`event: message.received\ndata: ${JSON.stringify(data)}\n\n`)
|
||||
},
|
||||
'message.sent': (data: any) => {
|
||||
res.write(`event: message.sent\ndata: ${JSON.stringify(data)}\n\n`)
|
||||
},
|
||||
'message.status': (data: any) => {
|
||||
res.write(`event: message.status\ndata: ${JSON.stringify(data)}\n\n`)
|
||||
}
|
||||
}
|
||||
|
||||
// Register listeners
|
||||
for (const [eventName, handler] of Object.entries(handlers)) {
|
||||
baileysManager.on(eventName, handler)
|
||||
}
|
||||
|
||||
// Keep-alive ping
|
||||
const pingInterval = setInterval(() => {
|
||||
res.write(`: ping\n\n`)
|
||||
}, 30000)
|
||||
|
||||
// Cleanup on close
|
||||
event.node.req.on('close', () => {
|
||||
clearInterval(pingInterval)
|
||||
for (const [eventName, handler] of Object.entries(handlers)) {
|
||||
baileysManager.off(eventName, handler)
|
||||
}
|
||||
})
|
||||
|
||||
// Don't close the connection
|
||||
return new Promise(() => {})
|
||||
})
|
||||
Reference in New Issue
Block a user