サイトのトップへ戻る

Twitter 開発者 ドキュメント日本語訳

ストリーミングメッセージのタイプ

  1. Public stream messages
    1. Blank Lines
    2. Status deletion notices (delete)
    3. Location deletion notices (scrub_geo)
    4. Limit notices (limit)
    5. Withheld content notices (status_withheld, user_withheld)
    6. Disconnect messages (disconnect)
    7. Stall warnings (warning)
    8. User update
  2. User stream messages
    1. Friends lists (friends)
    2. Direct Messages
    3. Events (event)
    4. Too many follows (warning)
  3. Site stream messages
    1. Envelopes (for user)
    2. Control messages(control)


公開ストリームメッセージ

標準のツイートペイロードに加えて、ストリーム上では後述する種類のメッセージが配信されます。 この一覧は全てを網羅していない可能性があるので注意してください — 将来的に追加のオブジェクトが導入される可能性があります。ツイートを解析する際には、予期せぬ形式のメッセージが来ても対処できるようにしてください。

ツイートを解析する場合、リツイートは他のツイート内にステータスとして格納されて流れていることに注意してください。正規表現を使ってツイートを取得した場合、ツイートそのものではなくその中にあるツイートに合致して取得した可能性があります。 経験則として、メッセージデータから情報を抽出するにはJSONパーサーを使用するのが良いでしょう。



空白行

ツイートの少ないストリームでは、クライアントやその他ネットワークが「ストリームが停止して接続を切断した」と誤認するのを防ぐため、いくつのかのメッセージが“キープアライブ”のシグナルとして働く 空白行になることがあります。



ステータスの削除通知 (delete)

これらのメッセージは、指定したツイートが削除されていることを示します。クライアントのプログラムではこのメッセージを遵守し、該当ツイートをメモリ、ストレージ、アーカイブから削除する処理を行ってください。削除対象となるツイートを受信するよりも先に、削除メッセージを受信するようなレアケースであってもです。

{
  "delete":{
    "status":{
      "id":1234,
      "id_str":"1234",
      "user_id":3,
      "user_id_str":"3"
    }
  }
}


位置情報の削除通知 (scrub_geo)

これらのメッセージは指定された範囲のツイートから位置情報データを削除しなければならないことを示します。 クライアントはこのメッセージを尊重し、該当するツイートからジオコードデータを削除しなければなりません。 ごくまれにですが、指定されているツイートが届くよりも前に、そのメッセージの削除通知が届いてしまうこともあります。

{
  "scrub_geo":{
    "user_id":14090452,
    "user_id_str":"14090452",
    "up_to_status_id":23260136625,
    "up_to_status_id_str":"23260136625"
  }
}


制限通知 (limit)

このメッセージは、取得された検索結果以外にも、現在の速度制限の上限を超えてしまったために配信されなかったツイートがあることを示します。 制限通知には接続が開始されてからこれまでに配信されなかったツイートの総数が保持されており、例えば、その検索ワードで本来どれくらいの検索結果が得られるかを特定するのに使用できます。 Note that the counts do not specify which filter predicates undelivered messages matched.

{
  "limit":{
    "track":1234
  }
}


Withheld content notices (status_withheld, user_withheld)

これらのメッセージは、表示されたツイートもしくはユーザーが検閲制限を受けていることを示します。withheld content noticesを参照してください。

status_withheld

これらのイベントは、ツイートIDを表すid フィールド、ユーザーを表す user_id フィールド、大文字で書かれた二文字国コードwithheld_in_countriesの集まりを保持しています。 以下の例では、対象のツイート(仮のIDです)がドイツとアルゼンチンで検閲制限を受けていることを表しています。

{
  "status_withheld":{
      "id":1234567890,
      "user_id":123456,
      "withheld_in_countries":["DE", "AR"]
  }
}

user_withheld

これらのイベントは、ユーザーを表す user_id フィールド、大文字で書かれた二文字国コードwithheld_in_countriesの集まりを保持しています。 以下の例では、対象のユーザー(仮のIDです)がドイツとアルゼンチンで検閲制限を受けていることを表しています。

{  
  "user_withheld":{
    "id":123456,
    "withheld_in_countries":["DE","AR"]
  }
}


切断メッセージ (disconnect)

ストリームは様々な理由によって切断されることがあります。このストリーミングAPIでは、ストリームが閉じられた理由の送信を行います。 切断の原因がネットワークの問題やクライアントの読み込み遅延だった場合は、このメッセージは受信できない可能性があるので注意してください。

{
  "disconnect":{
    "code": 4,
    "stream_name":"",
    "reason":""
  }
}

以下のテーブルには、想定されるステータスコードとその意味を一覧にしています。これ以外のコードが予告なく使用される場合もあります。

コード 名前 説明
1 Shutdown 配信元がシャットダウンされました (マシンを再起動した可能性があります)
2 Duplicate stream 同一のエンドポイントに対して非常に多く接続しました。
3 Control request Control streams was used to close a stream (applies to sitestreams).
4 Stall クライアントの読み込みが遅すぎるのでサーバから切断されました。
5 Normal クライアントから切断を開始したと思われます。
6 Token revoked ユーザーの認証トークンが取り消されました(applies to site and userstreams).
7 Admin logout 同一の資格情報を使って新しいストリームが接続されたので、最も古いストリームが切断されました。
8 内部で使うために予約されたコードです。クライアント外では使われません。
9 Max message limit このストリームは負のcountパラメータを使って接続し、全ての埋め戻しが配信された後に切断されました。
10 Stream exception 内部の問題によってストリームが切断されました。
11 Broker stall 内部の問題によってストリームが切断されました。
12 Shed load ストリームの接続しているサーバが高負荷状態になり、負荷分散のためにストリームが切断されました。通常通り再接続してください。


速度低下警告 (warning)

stall_warnings パラメータを使用してストリームに接続している場合は、現在の接続状態を示すステータス通知を受け取ることができます。 詳細情報については stall_warnings のドキュメントを参照してください。

{
  "warning":{
    "code":"FALLING_BEHIND",
    "message":"Your connection is falling behind and messages are being queued for delivery to you. Your queue is now over 60% full. You will be disconnected when the queue is full.",
    "percent_full": 60
  }
}

サイトストリームの場合は、警告メッセージはストリーム全体に適用され、for_user エンベロープに囲まれていないので注意してください。



User update

ユーザーがプロフィールを更新する度に、我々はユーザープロフィールが更新されたことを示す user_updateイベントをブロードキャストします。 コンテンツ内の"source"オブジェクトと "target"オブジェクトの内容は同じものです。

{
    "created_at": "Tue Aug 06 02:23:21 +0000 2013",
    "source": {
         ...
    },
    "target": {
      ...
    },
    "event": "user_update"
}


User stream messages



Friends lists (friends)

スーザーストリームの接続を確立すると、 Twitter は通常のメッセージ配信を始める前に preamble を送信します。この preamble にはユーザーのフレンドリストが含まれています。 これは、例えば以下のようにユーザーIDの配列として表されます:

{
  "friends":[
    1497,
    169686021,
    790205,
    15211564,
    ...
  ]
}

このメッセージは接続ごとに一回だけ送信されます。

stringify_friend_id パラメータが送信された場合、フレンドリストpreamble は(数字ではなく)文字列として取得されます。 これは、言語やライブラリで64ビット整数を実装するのが難しい場合に特に有効です。なぜなら、Twitterのユーザ数は増えるにつれてユーザーIDが32ビット整数で使用できる上限を超えてしまうからです。このパラメータが使用されている場合、上で書いたような friends 配列は送信されず、代わって friends_str 配列が送信されます。例えば:

{
  "friends_str": [
    "1497",
    "169686021",
    "790205",
    "15211564",
    ...
  ]
}


ダイレクトメッセージ

ダイレクトメッセージは送信者と受信者の両方に配信されますが、 perspectival 属性 (following, follow_request_sent, notifications) は常に falseとなります。



Events (event)

ツイートではないイベントに関する通知も、ユーザーストリームを介して送信されます。これらは、一般的に以下のような形式になります:

{
  "event":"EVENT_NAME",
  "created_at": "Sat Sep 4 16:10:54 +0000 2010",
  "target": TARGET_USER,
  "source": SOURCE_USER, 
  "target_object": TARGET_OBJECT
}

取得される値はイベントのタイプによって異なります、以下のイベントのタイプがストリーム上で配信されます:

説明 イベント名 Source Target Target Object
ユーザーがストリームの認証を解除した access_revoked * Deauthorizing user App owner client_application
ユーザーが誰かをブロックした block Current user Blocked user Null
ユーザーがブロックを解除した unblock Current user Unblocked user Null
ユーザーがツイートをお気に入りに登録した favorite Current userTweet author Tweet
ユーザーのツイートがお気に入りに登録された favorite Favoriting user Current user Tweet
ユーザーがツイートのお気に入りを解除した unfavorite Current user Tweet author Tweet
ユーザーのツイートがお気に入りから解除された unfavorite Unfavoriting user Current user Tweet
ユーザーが誰かをフォローした follow Current user Followed user Null
ユーザーがフォローされた follow Following user Current user Null
ユーザーが誰かのフォローを解除した unfollow Current user Followed user Null
ユーザーがリストを作成した list_created Current user Current user List
ユーザーがリストを削除した list_destroyed Current user Current user List
ユーザーがリストを編集した list_updated Current user Current user List
ユーザーが誰かをリストに追加した list_member_added Current user Added user List
ユーザーがリストに追加された list_member_added Adding user Current user List
ユーザーがリストから誰かを削除した list_member_removed Current user Removed user List
ユーザーがリストから削除された list_member_removed Removing user Current user List
ユーザーがリストを購読した list_user_subscribed Current user List owner List
ユーザーのリストが購読された list_user_subscribed Subscribing user Current user List
ユーザーがリストの購読を解除した list_user_unsubscribed Current user List owner List
ユーザーのリストの購読が解除された list_user_unsubscribed Unsubscribing user Current user List
ユーザーが自分のプロフィールを更新した user_update Current user Current user Null
ユーザーがプロテクト状態を更新した user_update Current user Current user Null

* For a user stream, if the user on behalf of whom the app is running deauthorizes the app, the stream will simply disconnect. For a site stream, if the deauthorizing user is the last remaining user on behalf of whom the app is running, the stream will send a disconnect message with code 6 and then close the connection.

When a change triggering a user_update event in the stream is made by a user, the stream returns complete profile information for the user, not just the changed values. At present, no data is returned indicating what values have changed; therefore, if your application requires such updates, you are advised to cache previous user profile information and compare user_update data against it.



Too many follows (warning)

User and Site Streams following graph size will be capped at 10,000 accounts for each connected user. 10,000以上のアカウントをフォローしているユーザーがあなたのアプリケーションを使って接続した場合、そのユーザーのフォロー一覧は除外されて以下のメッセージがストリームを介して送信されます:

{
  "warning": {
    "code": "FOLLOWS_OVER_LIMIT",
    "message": "The requested user follows more accounts than the maximum supported by this streaming endpoint. Only a subset of 10000 followed accounts are included in this stream.",
    "user_id": <user_id>
  }
}

The connected user’s Tweets, @replies, and social events for favorites and retweets will always be streamed. However, the 10,000 accounts that will be included are a random subset of the accounts the connected user follows. Any with=followings connections will only stream content from users in the truncated list. The IDs delivered via the Control Streams friends/ids.json endpoint will also only include IDs from users in the truncated list. If your application requires a full list of followings, please resort to the REST API.

Note that in the case of Site Streams warning messages apply to the entire stream and will not be wrapped with a for_user envelope.



Site stream messages



Envelopes (for_user)

Site Streams are sent the same messages as User Streams (including friends lists in the preamble), but for multiple users instead of a single user. The same types of messages are streamed, but to identify the target of each message, an additional wrapper is placed around every message, except for blank keep-alive lines. The Site Streams messages for two friends lists would look like:

{
  "for_user":1888,
  "message":{"friends":[]}
}
{
  "for_user":9160152,
  "message":{"friends":[]}
}

If a message should be routed to multiple users, multiple wrapped messages will be sent, each with a different for_user value.

Note that warning messages apply to the entire stream and will not be wrapped with a for_user envelope.

By default the user id is given as an integer, but you can also get the user id as a string by giving stringify_friend_ids as a parameter when connecting to the site stream. With stringify_friend_ids set to true, the example above would look like:

{
  "for_user_str":"1888",
  "message":{"friends":[]}
}
{
  "for_user_str":"9160152",
  "message":{"friends":[]}
}


制御メッセージ (control)

サイトストリームに新規接続すると、再接続せずにサイトストリームの接続を編集できるコントロールメッセージを取得します。 詳細については サイトストリームの制御ストリーム を参照してください。 Note that this message will not necessarily be the first message delivered on a Site Streams connection.

{
  "control":{
    "control_uri": "/1.1/site/c/01_225167_334389048B872A533002B34D73F8C29FD09EFC50"
  }
}