Cette rubrique décrit les différences entre le mode HTTP traditionnel et le mode Server-Sent Events (SSE). En mode HTTP traditionnel, chaque requête prend plus de temps à aboutir et ne renvoie qu’un seul paquet de données. Avec le protocole SSE, le serveur transmet plusieurs événements au client en temps réel dès que de nouvelles données sont disponibles. Le mode SSE convient particulièrement aux scénarios nécessitant des mises à jour en temps réel, comme la génération dynamique de texte.
Mode HTTP traditionnel
En mode traditionnel, chaque requête génère une seule réponse.
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Mon, 07 Aug 2023 09:44:02 GMT
Content-Type: application/json;charset=UTF-8
Connection: close
Vary: Accept-Encoding
x-oss-request-id: 64D0BCDD88339E32330E2D1D
x-oss-server-time: 5865
Content-Encoding: gzip
{
"RequestID": "64D0BCDD88339E32330E2D1D",
"Output": {
"Text": "A Ghost, the Communist Ghost, haunts the European continent. In order to holy suppression of this ghost, all forces of the old Europe, including the Pope and the Czar, Metternich and Guizot, the radical party in France and the police in Germany, have united. Is there any opposition party that is not denounced by its governing enemies as a Communist? And is there any opposition party that does not use the charge of Communism as a weapon against more progressive oppositionists and its reactionary enemies? From this fact we can draw two conclusions: Communism has been recognized by all the forces of Europe as a power; it is now the turn of the Communists to make themselves known to the world, to state their views, their aims, their intentions, and to publish their own manifestos in order to refute the myth about the Communist ghost. For this purpose, delegates of the Communist parties of all countries assembled in London and adopted the following manifesto, which is published in English, French, German, Italian, Flemish and Danish.",
"FinishReason": "stop"
}
}
Mode SSE
Le mode SSE se limite à une communication unidirectionnelle du serveur vers le client. L’interface EventSource fait partie de la norme HTML Living Standard (anciennement appelée HTML5).
Les données transmises du serveur au client en mode SSE sont encodées en UTF-8 et au format texte brut. Elles circulent sous forme de flux de messages d’événements séparés par deux sauts de ligne (\n\n). Chaque message d’événement comprend plusieurs lignes formatées en paires clé-valeur [key]: value\n. Les clés facultatives sont id, event et data. Le champ data contient le contenu d’un message.
La valeur de la clé data est un objet JSON qui contient les paramètres de réponse.
Exemple de réponse :
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Mon, 07 Aug 2023 09:44:36 GMT
Content-Type: text/event-stream;charset=UTF-8
Transfer-Encoding: chunked
Connection: close
x-oss-request-id: 64D0BD0388339E30331D2D1D
x-oss-server-time: 761
id: 0
event: message
data: {"RequestId":"","Output":{"Text":"A Ghost,","FinishReason":"null"},"Usage":{"InputTokens":320,"OutputTokens":3}}
id: 1
event: message
data: {"RequestId":"","Output":{"Text":"A Ghost, the Communist Ghost, haunts the European","FinishReason":"null"},"Usage":{"InputTokens":320,"OutputTokens":11}}
id: 2
event: message
data: {"RequestId":"","Output":{"Text":"A Ghost, the Communist Ghost, haunts the European continent. In order to holy suppression of","FinishReason":"null"},"Usage":{"InputTokens":320,"OutputTokens":19}}
......
id: 27
event: Result
data: {"RequestId":"","Output":{"Text":"A Ghost, the Communist Ghost, haunts the European continent. In order to holy suppression of this ghost, all forces of the old Europe, including the Pope and the Czar, Metternich and Guizot, the radical party in France and the police in Germany, have united. Is there any opposition party that is not denounced by its governing enemies as a Communist? And is there any opposition party that does not use the charge of Communism as a weapon against more progressive oppositionists and its reactionary enemies? From this fact we can draw two conclusions: Communism has been recognized by all the forces of Europe as a power; it is now the turn of the Communists to make themselves known to the world, to state their views, their aims, their intentions, and to publish their own manifestos in order to refute the myth about the Communist ghost. For this purpose, delegates of the Communist parties of all countries assembled in London and adopted the following manifesto, which is published in English, French, German, Italian, Flemish and Danish.","FinishReason":"stop"},"Usage":{"InputTokens":320,"OutputTokens":212}}
Chaque message contient des données incrémentielles, ce qui signifie que les données s’enrichissent ou se modifient progressivement au fil de la requête.