April 22, 2012

jQuery LiveTwitter

= jQuery LiveTwitter

LiveTwitter is a lightweight, live updating Twitter plugin for jQuery. You can use it to show a stream of tweets based on search queries, or tweets from a specific user or list. It also supports StatusNet/identi.ca.

== Basic usage

You'll need a container for the stream of tweets:

Populating it is as simple as:

$("#tweets").liveTwitter('bacon');

You can also show tweets from a user's timeline:

$("#tweets").liveTwitter('elektronaut', {mode: 'user_timeline'});

..or a list:

$("#tweets").liveTwitter({user: 'jquery', list: 'team'}, {mode: 'list'});

..or a user's favorites:

$("#tweets").liveTwitter('elektronaut', {mode: 'favorites'});

Changing the query or options is pretty easy, just call liveTwitter again with the new parameters:

$("#tweets").liveTwitter('salad'); $("#tweets").liveTwitter('celery', {limit: 25}); $("#tweets").liveTwitter(false, {limit: 25});

== Options

=== Global options

mode: Valid options are 'search', 'user_timeline', 'list', 'favorites'. 'search' is the default. rate: Refresh rate, in milliseconds. The default is 15000. limit: Maximum number of tweets to show at once. The default is 10. imageSize: Dimensions of profile image. The default is 24 pixels. refresh: Pass refresh: false to disable automatic refreshing. rpp: Results per page to request from the API. This defaults to the limit, increase it if you're filtering tweets. showAuthor: Show username and profile picture. Defaults to false for user_timeline and true for all other modes. filter: Pass a function to filter tweets before displaying them. service: Use another service with compatible API, for example 'identi.ca' or 'youraccount.status.net'. timeLinks: Pass timeLinks: false to disable the timestamp on tweets. localization: Hash for localization strings, see example.

=== Options for search

geocode: Search tweets near a specific location. Example: '59.919151,10.749950,50km' lang: Only show tweets in this language.

See the Twitter Search API for info on how geocode and lang works: http://search.twitter.com/api

=== Options for user_timeline

retweets: Includes native retweets. Disabled by default. replies: Includes replies. Enabled by default.

=== Localization

The timestamp text is in english by default, but can be localized. Here's an example for Norwegian:

$('#tweets').liveTwitter('bacon', {localization: { seconds: 'sekunder siden', minute: 'ett minutt siden', minutes: 'minutter siden', hour: 'en time siden', hours: 'timer siden', day: 'en dag siden', days: 'dager siden' }});

== A note on rate limiting

The Twitter API enforces a limit on how many requests you can perform in a certain timeframe. The search API has a rather liberal rate limit, but the other modes are noticeably harsher. I recommend setting a higher limit (like 300000) if you're displaying a user timeline.

If you're constantly refreshing the page while developing and suddenly start seeing tweets dropping out, this is probably the reason.

== Advanced usage

Stopping, starting and refreshing manually:

$("#tweets").each(function(){ this.twitter.stop(); }); $("#tweets").each(function(){ this.twitter.start(); }); $("#tweets").each(function(){ this.twitter.refresh(); });

If you want to clear the tweets on changing query, you can do this:

$("#tweets").each(function(){ this.twitter.clear(); }).liveTwitter('my new query');

=== Filtering tweets

You can filter tweets by providing a function:

$('#tweets').liveTwitter('bacon', {limit: 10, rpp: 100, filter: function(tweet){ return ($.inArray(tweet.iso_language_code, ['no', 'sv']) > -1); // Show only norwegian and swedish tweets }});

=== Callbacks and events

If you want to apply behavior when new tweets are loaded, you can pass a callback function:

$('#tweets').liveTwitter('bacon', {}, function(container, newCount){ alert(newCount + ' new tweets loaded!'); });

..or if events are more your style:

$('#tweets').liveTwitter('bacon').bind('tweets', function(){ alert('New tweets!'); });

== License:

(The MIT License)

Copyright (c) 2009 Inge Jørgensen

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.