すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:従来のHTTPモードとSSE

最終更新日:Mar 19, 2025

このトピックでは、従来のHTTPモードとServer-Sent Events(SSE)モードの違いについて説明します。従来のHTTPモードでは、各リクエストの完了に時間がかかり、リクエストごとに1つのデータパケットのみが返されます。 SSEを使用すると、新しいデータが利用可能になったときに、サーバーからクライアントに複数のイベントをリアルタイムで送信できます。比較すると、SSEモードは、動的テキスト生成などのリアルタイム更新を伴うシナリオに特に適しています。

従来のHTTPモード

従来のモードでは、各リクエストは1つのレスポンスパケットを生成します。

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. 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 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"
  }
}

SSEモード

SSEモードは、サーバーからクライアントへの一方向通信に限定されます。 EventSource インターフェースは、HTML Living Standard(旧称 HTML5)の一部です。

SSEモードでサーバーからクライアントに送信されるデータは、UTF-8 を使用してエンコードされ、プレーンテキスト形式です。 データは、2つの改行文字(\n\n)で区切られたイベントメッセージのストリームとして送信されます。 各イベントメッセージは、キーと値のペア [key]: value\n としてフォーマットされた複数の行で構成されます。 オプションのキーは、id、event、および data です。 data フィールドには、メッセージの内容が含まれています。

data キーの値は、レスポンスパラメータを含む JSON オブジェクトです。

レスポンスの例:

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. 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. 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 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}}

各メッセージには増分データが含まれており、データが各リクエストの一部として徐々に充実または変更されることを示しています。