TwitterStreamListener Best Practice

When using the TwitterStreamListener activity, follow the guidelines to optimize the system resources and avoid situations where Twitter closes the stream.

The following are the situations where Twitter closes the stream or interferes with your application. To avoid these situations, take note of the stream behaviors and following the rules when creating listeners.

Public Stream

When listening to the public stream, conform to the following rules when creating listeners.
Situations Solutions/Suggestions/Reasons
The oldest connection is terminated when a client establishes too many connections with the same credentials. Do not run two clients in parallel with the same credentials.
A client suddenly stops reading data.

When the rate of Tweets being read off of the stream drops suddenly, the connection is closed.

This occurs when the client drops the speed of reading.
A client reads data too slow.

Every streaming connection is backed by a queue of messages to be sent to the client. When the queue grows too large over time, the connection is closed.

Reduce the number of Tweets hitting a queue by using appropriate parameters.
  • Track: When you retrieve specific Tweets, do not use generic or broad keywords, like a, b, c, or d. Instead, use Tibco, Tibbr, Bestbuy. Therefore, you can receive specific Tweets and avoid the queue being flooded.
  • Location: When you retrieve Tweets from the specific location, use boundary coordinates of that place instead of listening to the Tweets created all over the world.
A streaming server has been restarted. The plug-in automatically reconnects to the server when this situations occurs. However, if it fails after multiple attempts, manual restart is required.
Repeatedly opening and closing a connection can cause the stream listener to close Keep your connections as stable and long-lived as possible.
Clients who make excessive connection attempts (both successful and unsuccessful) run the risk of having their IP automatically banned by Twitter. Avoid getting your IP banned by not making excessive connection attempts.

User Stream

When listening to the user stream, conform to the following rules when creating listeners.
Situations Solutions/Suggestions/Reasons
Each Twitter account is limited to only a few simultaneous User Streams connections per OAuth application regardless of the IP. Minimize the number of connections your application makes to User Streams.
Twitter bans the account temporarily from User Streams for an excessive login rate or starting and stopping frequently. Avoid logging in with same account too often.
Number of simultaneous connections per IP address is still limited, regardless of application. Do not use multiple connections from a same IP.