Code Monkey home page Code Monkey logo

twitter-stream-api's Introduction

twitter-stream-api

DependenciesBuild Status

A streaming Twitter Stream API client with extended exposure of the underlaying protocol events. It does also fully adhere to Twitters different reconnect rules.

Installation

$ npm install twitter-stream-api

Simple stream usage

Connect to the Twitter stream API and listen for messages containing the word "javascript".

var TwitterStream = require('twitter-stream-api'),
    fs = require('fs');

var keys = {
    consumer_key : "your_consumer_key",
    consumer_secret : "your_consumer_secret",
    token : "your_access_token_key",
    token_secret : "your_access_token_secret"
};

var Twitter = new TwitterStream(keys, false);
Twitter.stream('statuses/filter', {
    track: 'javascript'
});

Twitter.pipe(fs.createWriteStream('tweets.json'));

Constructor

Create a new Twitter Stream API instance.

var Twitter = new TwitterStream(keys, [objectMode, options]);

keys (required)

Takes an Object containing your Twitter API keys and access tokens. The Object are as follow:

{
    consumer_key : "your_consumer_key",
    consumer_secret : "your_consumer_secret",
    token : "your_access_token_key",
    token_secret : "your_access_token_secret"
}

Twitter API keys and tokens can be generated here.

objectMode (optional)

Boolean value for controlling if the stream should emit Objects or not. Default value is true which set the stream to emit Objects. If a non-object stream is wanted, set the value to false.

options (optional)

An Object containing misc configuration. The following values can be provided:

  • gzip - Boolean value for enabling / disabling gzip on the connection against Twitter.
  • pool - Sets pool configuration on the underlaying request.js object.

Please refere to request.js for further documentation on these cunfiguration options.

API

The Twitter Stream API instance have the following API:

.stream(endpoint, parameters)

Opens a connection to a given stream endpoint.

endpoint (required)

The following values can be provided:

parameters (required)

Object holding optional Twitter Stream API endpoint parameters. The Twitter Stream API endpoints can take a set of given parameters which can be found in the API documentation for each endpoint.

Example:

The statuses/filter endpoint can take a track parameter for tracking tweets on keywords. The same endpoint can also take a stall_warnings parameter to include stall warnings in the Twitter stream.

To track the keyword javascript and include stall warnings, do as follow:

Twitter.stream('statuses/filter', {
    track: 'javascript',
    stall_warnings: true
});

Do note that the track and follow parameters can take both a comma separated list of values or an Array of values.

Twitter.stream('statuses/filter', {
    track: 'javascript,rust'
});

is the same as:

Twitter.stream('statuses/filter', {
    track: ['javascript','rust']
});

.close()

Closes the connection against the Twitter Stream API.

.debug(callback)

Under the hood this client use request to connect to the Twitter Stream API. Request have several tools for debugging its connection(s). This method provide access to the underlaying request object so one can plug in a debugger to request.

The underlaying request object are available as the first argument on the callback.

Example using request-debug:

var Twitter = new TwitterStream(keys);

Twitter.debug(function (reqObj) {
    require('request-debug')(reqObj, function (type, data, req) {
        console.log(type, data, req);
    });
});

Events

twitter-stream-api expose a rich set of events making it possible to monitor and take action upon what is going on under the hood.

connection success

Emitted when a successfull connection to the Twitter Stream API are established.

Twitter.on('connection success', function (uri) {
    console.log('connection success', uri);
});

connection aborted

Emitted when a the connection to the Twitter Stream API are taken down / closed.

Twitter.on('connection aborted', function () {
    console.log('connection aborted');
});

connection error network

Emitted when the connection to the Twitter Stream API have TCP/IP level network errors. This error event are normally emitted if there are network level errors during the connection process.

Twitter.on('connection error network', function (error) {
    console.log('connection error network', error);
});

When this event is emitted a linear reconnect will start. The reconnect will attempt a reconnect after 250 milliseconds and increase the reconnect attempts linearly up to 16 seconds.

connection error stall

Emitted when the connection to the Twitter Stream API have been flagged as stall. A stall connection is a connection which have not received any new data or keep alive messages from the Twitter Stream API during a period of 90 seconds.

This error event are normally emitted when a connection have been established but there has been a drop in it after a while.

Twitter.on('connection error stall', function () {
    console.log('connection error stall');
});

When this event is emitted a linear reconnect will start. The reconnect will attempt a reconnect after 250 milliseconds and increase the reconnect attempts linearly up to 16 seconds.

connection error http

Emitted when the connection to the Twitter Stream API return an HTTP error code.

This error event are normally emitted if there are HTTP errors during the connection process.

Twitter.on('connection error http', function (httpStatusCode) {
    console.log('connection error http', httpStatusCode);
});

When this event is emitted a exponentially reconnect will start. The reconnect will attempt a reconnect after 5 seconds and increase the reconnect attempts exponentially up to 320 seconds.

connection rate limit

Emitted when the connection to the Twitter Stream API are being rate limited. Twitter does only allow one connection for each application to its Stream API.Multiple connections or to rappid reconnects will cause a rate limiting to happen.

Twitter.on('connection rate limit', function (httpStatusCode) {
    console.log('connection rate limit', httpStatusCode);
});

When this event is emitted a exponentially reconnect will start. The reconnect will attempt a reconnect after 1 minute and double the reconnect attempts exponentially.

connection error unknown

Emitted when the connection to the Twitter Stream API throw an unexpected error which are not within the errors defined by the Twitter Stream API documentation.

Twitter.on('connection error unknown', function (error) {
    console.log('connection error unknown', error);
    Twitter.close();
});

When this event is emitted the client will, if it can, keep the connection to the Twitter Stream API and not attemt to reconnect. Closing the connection and handling a possilbe reconnect must be handled by the consumer of the client.

data

Emitted when a Tweet ocur in the stream.

Twitter.on('data', function (obj) {
    console.log('data', obj);
});

data keep-alive

Emitted when the client receive a keep alive message from the Twitter Stream API. The Twitter Stream API sends a keep alive message every 30 second if no messages have been sendt to ensure that the connection are kept open. This keep alive messages are mostly being used under the hood to detect stalled connections and other connection issues.

Twitter.on('data keep-alive', function () {
    console.log('data keep-alive');
});

data error

Emitted if the client received an message from the Twitter Stream API which the client could not parse into an object or handle in some other way.

Twitter.on('data error', function (error) {
    console.log('data error', error);
});

License

The MIT License (MIT)

Copyright (c) 2015 - Trygve Lie - [email protected]

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.

twitter-stream-api's People

Contributors

greenkeeperio-bot avatar trygve-lie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

twitter-stream-api's Issues

TypeError: Twitter.destroy is not a function

Twitter.destroy() throws the following error.

uncaughtException Twitter.destroy is not a function
TypeError: Twitter.destroy is not a function

I am calling destroy after a making a successful connection to a twitter stream.

Twitter.close(); doesn't kill the streaming connection because keep-alive messages and tweets continue to come through.

Track all mentions of a particular domain name

Hi ! :-)

Thanks for your useful package!

I would like to known if you have already had problem to track all mentions of a particular domain (i.e., regardless of subdomain or path) ?

As explained in the Twitter API, if we want to match all "http://www.example.com/whatever/", we need to track "example com", but this doesn't seems to works.

If I use "example.com" this works, but of course only for tweets with this exact URL ( don't match example.com/whatever/ )

Thanks,
Regards

Make it possible to configure the request object

request have a set of configuration which can be set. Some of those are of interest when connecting to the twitter stream api.

I have recently come across a need to adjust the request connection pool on the request object. We should expose a way to set config on the request object.

ERROR in ./node_modules/tunnel-agent/index.js

I am trying to connect to twitter-stream-api.
but i am getting this error.

ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\User\Desktop\hyojin\simp le-view-tweets\node_modules\tunnel-agent'

I am using React, Redux !
Would you give me any advice ?

Thank you.

Getting HTTP 431 error when trying to follow more than 1000 users.

According to the twitter docs you can filter up to 5000 user ids on the standard stream API. I am currently porting my app over from Python where I was using Tweepy and had no problem with this. However when I try it now I get a 431 error (Request Header Fields Too Large).
My code works when I do this:
twitter.stream('statuses/filter', { follow: users.slice(0,993)});
and then breaks when I do this:
twitter.stream('statuses/filter', { follow: users.slice(0,994)});

If anyone has any insight I'd really appreciate it. Thanks!

statuses/followers caused a problem: "Cannot read property 'uri' of undefined"

Code:

Twitter.stream( 
	'statuses/followers', 
	{ 
		track: 'javascript' 
	}
);

gives me an error:

.../node_modules/twitter-stream-api/lib/connection.js:53
    var uri = endpoints[endpoint].uri + '?' + querystring.stringify(params),
                                 ^

TypeError: Cannot read property 'uri' of undefined
    at module.exports.Connection.connect (node_modules/twitter-stream-api/lib/connection.js:53:34)
    at module.exports.Twitter.stream (node_modules/twitter-stream-api/lib/main.js:186:21)
    at Object.<anonymous> (test-twitter.js:14:9)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)

Connection aborted

I'm trying 2 different twitter stream api and I can't connect with twitter-stream-api.

I'm using the same credentials but keep having connection aborted only for this library.
Any idea what could cause this ?

Do not allow empty properties in the connection config object

We should control that the follow / track parameters in the connection config object we send to the connection does not contain empty values.

From experience it seems like:

If a connection is created with both follow and track is empty, twitter will disconnect immediately with a 406 and finally ban.

If one is given a value and the second is not, one will be connected but after a while twitter will start disconnecting with 401 messages.

Not 100% sure if this is consequent, but have experienced this behaviour on several streams I use this lib on.

We should check the connection config object and remove the parameter if it has an empty value.

Enable gzip

Make it possible to enable gzip on the request to the Twitter stream API.

Stall detection is entirely non-functional

I've had a few cases now where the "keepalive" messages (which I log when I find a data keep-alive event) stop coming, and tweets I expect to receive are not received. There aren't other tweets or events coming in during these periods (I log something for every event I'm aware of). The process is definitely still running.

I've never seen any of the connection error * events happen -- I wonder if there's a bug somewhere.

A snippet from my code:

ts.on('connection aborted', function tsConnectionAborted() {
        console.error("Twitter stream: connection aborted");
});
ts.on('connection error network', function tsConnectionErrorNetwork(error) {
        console.error("Twitter stream: connection network error:", error);
});
ts.on('connection error stall', function tsConnectionErrorStall() {
        console.error("Twitter stream: connection stalled");
});
ts.on('connection error http', function tsConnectionErrorHttp(httpStatusCode) {
        console.error("Twitter stream: connection HTTP error:", httpStatusCode);
});
ts.on('connection error unknown', function tsConnectionErrorUnknown(error) {
        console.error("Twitter stream: connection error:", error);
});
ts.on('data keep-alive', function tsKeepAlive() {
        console.log("Twitter stream: keepalive");
});
ts.on('data error', function tsDataError(error) {
        console.error("Twitter stream: data error:", error);
});

Out of these, the only one I've ever seen is the "keepalive", and they come pretty often in the logs. The most recent time it was missing was for a period of about an hour, after which at least two matching tweets were missed, and then I restarted the process.

What can I do to help debug?

Consider exposing more connection events

From private debugging sessions of Twitter stream connection issues, these events would might come in handy:

  • connection closed
  • stream end
  • request close
  • reconnect start
  • reconnect aborted

Errors using pipe

So I've been able to get streaming data but when I do something like:

var Twitter = new TwitterStream(keys);
var outFile = require('fs').createWriteStream('out.json');

Twitter.stream('statuses/sample');

Twitter.on('error', function (err) {
  console.log(err.message);
  console.log(err.stack);
});

Twitter.pipe(outFile);

I always tend to get something like:

events.js:85
      throw er; // Unhandled 'error' event
            ^
TypeError: Invalid non-string/buffer chunk
    at validChunk (_stream_writable.js:186:14)
    at WriteStream.Writable.write (_stream_writable.js:215:12)
    at ondata (.../twitter-stream-api/node_modules/readable-stream/lib/_stream_readable.js:574:20)
    at emit (events.js:107:17)
    at readableAddChunk (.../twitter-stream-api/node_modules/readable-stream/lib/_stream_readable.js:198:16)
    at Readable.push (.../twitter-stream-api/node_modules/readable-stream/lib/_stream_readable.js:162:10)
    at null.<anonymous> (.../twitter-stream-api/lib/main.js:43:19)
    at emit (events.js:107:17)
    at null.<anonymous> (.../twitter-stream-api/lib/connection.js:37:18)
    at emit (events.js:107:17)

BUT!

If instead I listen on the data event for the Stream, the data comes through just fine. For completeness, if I use the same code above but instead of piping to the outFile stream I do this:

Twitter.on('data', function(data) {
  console.log(data);
});

No errors, data flows normally.

Set proper User Agent

Send proper User Agent when connecting according to the Twitter doc:

"Ensure your User-Agent HTTP header includes the client’s version. This will be critical in diagnosing issues on Twitter’s end. If your environment precludes setting the User-Agent field, then set an X-User-Agent header."

https://dev.twitter.com/streaming/overview/connecting

Tweet deletion notifications as per the Twitter docs

Hi there,

Thanks for creating and maintaining this repo, it is awesome! I have looked through the docs, and I don't see any explicit support for status deletions?

I'm referring to these docs: https://dev.twitter.com/streaming/overview/messages-types#status_deletion_notices_delete.

Is there an event that provides this kind of info? If not, ideally, there should be some kind of event the twitter stream instance fires on a deletion notice, like "data delete". Let me know if you need help implementing this, and I can put together a PR.

Cheers,
Dhruv

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.