🔔

Documentation API Socket.IO ChapFood

🔌 Connexion

ParamĂštres de connexion

http://localhost:8001/
/socket.io/
/
5 tentatives · délai 1 s
Exemple
const socket = io('/', {
  path: '/socket.io/',
  auth: { user_id: 'user_42', username: 'Alice' },
  reconnection: true,
  reconnectionAttempts: 5,
  reconnectionDelay: 1000,
});
đŸ“„ ÉvĂ©nements Ă  Ă©couter (server → client)
connection-confirmed Confirmation de connexion au serveur listen
Payload
{
  "socketId":   "abc12345",
  "serverTime": "2026-06-07T10:00:00.000Z"
}
Exemple
socket.on('connection-confirmed', (data) => {
  console.log('Socket ID:', data.socketId);
});
notification-received Notification reçue (broadcast ou channel ciblé) listen
Payload
{
  "id":        "n_1749290400.123",
  "type":      "info",       // info | success | warning | error
  "title":     "Mon titre",
  "message":   "Mon message",
  "data":      { "event": "order.created", "ref": "ORD-42" }, // null si absent
  "sender":    "Alice",
  "channel":   "devops",    // null si broadcast
  "timestamp": "2026-06-07T10:00:00.000Z"
}
info success warning error
Exemple
socket.on('notification-received', (n) => {
  console.log(`[${n.type}] ${n.title}: ${n.message}`);
  if (n.data) console.log('Data:', n.data);
});
notification-sent Confirmation d'envoi (renvoyée uniquement à l'émetteur) listen
Payload
{
  "notificationId": "n_1749290400.123",
  "channel":        "devops",  // null si broadcast
  "timestamp":      "2026-06-07T10:00:00.000Z"
}
Exemple
socket.on('notification-sent', (data) => {
  console.log('✅ EnvoyĂ©, id:', data.notificationId);
});
channel-joined Confirmation de rejoindre un channel listen
Payload
{
  "channel":      "devops",
  "message":      "Vous avez rejoint « devops »",
  "clientCount":  3,
  "pendingCount": 0,
  "timestamp":    "2026-06-07T10:00:00.000Z"
}
channel-left Confirmation de quitter un channel listen
Payload
{
  "channel":   "devops",
  "message":   "Vous avez quitté « devops »",
  "timestamp": "2026-06-07T10:00:00.000Z"
}
channels-list Liste des channels actifs (réponse à get-channels) listen
Payload
[
  { "name": "devops",  "clientCount": 3 },
  { "name": "orders",  "clientCount": 1 }
]
Exemple
socket.on('channels-list', (channels) => {
  channels.forEach(c => console.log(c.name, c.clientCount));
});
server-stats Statistiques serveur — Ă©mis Ă  chaque changement d'Ă©tat et en rĂ©ponse Ă  get-stats listen
Payload
{
  "connectedClients": 5,
  "activeChannels":   2,
  "uptime":           3600.42,  // secondes
  "timestamp":        "2026-06-07T10:00:00.000Z"
}
error-message Erreur de validation ou d'exécution listen
Payload
{
  "errors": ["Le champ 'title' est obligatoire", "..."]
}
Exemple
socket.on('error-message', (err) => {
  err.errors.forEach(e => console.error(e));
});
đŸ“€ ÉvĂ©nements Ă  Ă©mettre (client → server)
join-channel Rejoindre un channel pour recevoir ses notifications emit
Payload
"nom-du-channel"  // string directement, pas un objet
Exemple
socket.emit('join-channel', 'devops');
leave-channel Quitter un channel emit
Payload
"nom-du-channel"  // string directement
Exemple
socket.emit('leave-channel', 'devops');
get-stats Demander les statistiques serveur (réponse via server-stats) emit
Payload
// aucun payload
Exemple
socket.emit('get-stats');
get-channels Demander la liste des channels actifs (réponse via channels-list) emit
Payload
// aucun payload
Exemple
socket.emit('get-channels');
send-notification Envoyer une notification (broadcast si channel absent) emit
Payload
{
  "channel": "devops",       // optionnel — absent = broadcast
  "type":    "info",          // info | success | warning | error
  "title":   "Mon titre",     // obligatoire
  "message": "Mon message",   // obligatoire
  "data": {                    // optionnel, objet libre
    "event":     "order.created",
    "reference": "ORD-123",
    "custom":    "data"
  }
}
Exemple — channel ciblĂ© avec data
socket.emit('send-notification', {
  channel: 'devops',
  type:    'warning',
  title:   'Alerte CPU',
  message: 'CPU Ă  90 % sur prod-01',
  data: { host: 'prod-01', metric: 'cpu', value: 90 },
});
💡 Exemple complet
client.js
const socket = io('/', {
  path: '/socket.io/',
  reconnection: true,
  reconnectionAttempts: 5,
  reconnectionDelay: 1000,
});

socket.on('connect', () => {
  socket.emit('join-channel', 'notifications-app');
  socket.emit('get-stats');
});

socket.on('notification-received', (notif) => {
  console.log(`[${notif.type}] ${notif.title}: ${notif.message}`);
  console.log('Data:', notif.data);
});

socket.on('error-message', (err) => {
  console.error('Erreurs:', err.errors);
});