Este proyecto demuestra el uso de GraphQL con suscripciones en tiempo real (WebSocket) y Server-Sent Events (SSE) para crear una aplicación de eventos en tiempo real, mostrando ambos enfoques en paralelo.
- Node.js (versión 14 o superior)
- npm (incluido con Node.js)
- Clona el repositorio o descarga los archivos
- Instala las dependencias del servidor:
npm install- Inicia el servidor:
npm start- Abre el navegador en
http://localhost:3000para ver la interfaz de usuario
- Abre
http://localhost:3000en tu navegador - Verás dos tablas: una para eventos GraphQL y otra para eventos SSE
- Cada tabla tiene un botón para enviar un evento de prueba
- El estado de la conexión WebSocket se muestra en la parte superior
- GraphQL (WebSocket y HTTP):
http://localhost:3000/graphql
- SSE (recibir eventos):
GET http://localhost:3000/events-sse
- Crear evento SSE:
POST http://localhost:3000/create-sse-event- Body JSON:
{ "message": "Texto del evento" }
- Body JSON:
- Obtener historial de eventos SSE:
GET http://localhost:3000/sse-events
- Abre
http://localhost:3000/graphqlen tu navegador - Puedes probar consultas, mutaciones y suscripciones
subscription {
eventCreated {
id
time
message
}
}mutation {
createEvent(message: "¡Hola desde GraphQL!") {
id
time
message
}
}curl -X POST -H "Content-Type: application/json" -d '{"message": "¡Hola desde SSE!"}' http://localhost:3000/create-sse-event- Método: POST
- URL:
http://localhost:3000/create-sse-event - Body: raw, JSON
{
"message": "Esto es un evento SSE desde Postman"
}.
├── public/
│ ├── index.html # Interfaz de usuario
│ ├── app.js # Código del cliente
│ └── styles.css # Estilos
├── server.js # Servidor principal (GraphQL + SSE)
├── schema.js # Esquema GraphQL
└── package.json # Dependencias
-
Backend:
- Node.js
- Express
- Apollo Server
- GraphQL
- WebSocket
- Server-Sent Events (SSE)
-
Frontend:
- Apollo Client
- GraphQL Subscriptions
- ES Modules
- Suscripciones en tiempo real usando WebSocket (GraphQL)
- Eventos en tiempo real usando SSE
- Interfaz de usuario con dos tablas independientes
- Botones de prueba para ambos métodos
- Reconexión automática para SSE
- Manejo de errores
- Puedes comparar fácilmente la experiencia y latencia entre GraphQL (WebSocket) y SSE
- Cada tabla solo muestra los eventos de su propio canal
- El sistema maneja automáticamente la reconexión si se pierde la conexión SSE