Webhooks, SSE y WebSockets
Estas técnicas resuelven direcciones diferentes:
| Técnica | Dirección | Caso |
|---|---|---|
| Webhook | Servidor externo → tu servidor | Pago confirmado |
| SSE | Servidor → navegador sobre HTTP | Progreso o tokens de IA |
| WebSocket | Bidireccional y persistente | Colaboración o juego |
Recibir un webhook
Sección titulada «Recibir un webhook»Un webhook se considera una entrada no confiable. Verifica su firma sobre el cuerpo crudo, fecha y secreto compartido antes de parsear o procesar.
const expected = createHmac("sha256", secret) .update(`${timestamp}.${rawBody}`) .digest("hex")
if (!timingSafeEqual(Buffer.from(signature), Buffer.from(expected))) { throw new UnauthorizedError()}Comprueba tolerancia de tiempo para reducir replay, conserva el identificador del evento para deduplicar y responde rápido. El procesamiento largo se envía a una cola. No confíes únicamente en IPs porque pueden cambiar o atravesar proxies.
Server-Sent Events
Sección titulada «Server-Sent Events»SSE (Server-Sent Events) envía eventos de texto por una respuesta HTTP mantenida abierta:
Content-Type: text/event-streamCache-Control: no-cache
id: 42event: progressdata: {"percent":60}Define heartbeat, cancelación y reconexión. Last-Event-ID puede ayudar a reanudar cuando los eventos se conservan. Algunos proxies agrupan buffers; comprueba la infraestructura real.
WebSocket
Sección titulada «WebSocket»WebSocket permite mensajes en ambos sentidos, pero exige autenticar la conexión, autorizar cada canal, limitar tamaño y frecuencia, gestionar presencia y manejar múltiples instancias mediante un sistema compartido. No lo elijas solo por ser “tiempo real”: para notificaciones unidireccionales, SSE suele ser más simple.