Giter VIP home page Giter VIP logo

user-stream's Introduction

user-stream

NPM module

Simple Node.js Twitter (API 1.1) user stream client (https://dev.twitter.com/docs/streaming-apis/streams/user)

NPM

Install

npm install user-stream

Usage

var Stream = require('user-stream');
var stream = new Stream({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
});

//create stream
stream.stream();

//listen stream data
stream.on('data', function(json) {
  console.log(json);
});

Events

  • data - stream data in JSON format
  • garbage - stream data who can't be parsed to JSON
  • close - stream close event (stream connection closed)
  • error - error event (request error, response error, response status code greater than 200)
  • connected - stream created
  • heartbeat - twitter emitted heartbeat

Methods

  • stream - create stream connection
  • destroy - destroy/close stream connection

Streaming API request parameters link

var params = {
    with: 'user'
}
//create stream
stream.stream(params);

Reserved parameters for lib

  • delimited
  • stall_warnings

user-stream's People

Contributors

aivis 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  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  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

user-stream's Issues

Fetch API cannot load https://userstream.twitter.com/1.1/user.json. Response for preflight has invalid HTTP status code 400

I'm using Chrome. Installed the package via npm and used browserify.

I've followed the instructions and I always get Fetch API cannot load https://userstream.twitter.com/1.1/user.json. Response for preflight has invalid HTTP status code 400

In fact, I've tried other packages and I get the same error. I can use curl from the command line to consume this endpoint, the one Twitter gives me from their OAuth signature generator.

Also, right before that message, it shows OPTIONS https://userstream.twitter.com/1.1/user.json

Is it because I'm testing locally?

garbage occurs too often.

{"created_at":"Sat Dec 17 08:37:10 +0000 2016","id":810041133732597760,"id_str":"810041133732597760","text":"garbage","source":"\u003ca href="http://twitter.com" rel="nofollow"\u003eTwitter Web Client\u003c/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":794036182753513472,"id_str":"794036182753513472","name":"Tweetaster","screen_name":"Tweetaster","location":"\ub300\ud55c\ubbfc\uad6d","url":null,"description":null,"protected":false,"verified":false,"followers_count":6,"friends_count":9,"listed_count":0,"favourites_count":9,"statuses_count":70,"created_at":"Thu Nov 03 04:39:13 +0000 2016","utc_offset":32400,"time_zone":"Seoul","geo_enabled":true,"lang":"ko","contributors_enabled":false,"is_translator":false,"profile_backgroun (The data is truncated here... so, it goes to garbage.)

Is it twitter's problem? or is it resolvable?

implement stringify_friend_ids

Now that Twitter is rolling out 64bit user IDs, it might be beneficial to receive IDs as strings whenever possible. On the user stream end point, it defaults to stringify_friend_ids=false, which returns an array IDs of numbers, whereas stringify_friend_ids=true returns an array of IDs as strings.

https://dev.twitter.com/docs/streaming-apis/parameters#stringify_friend_ids

Friends lists (friends)

Upon establishing a User Stream connection, Twitter will send a preamble before starting regular message delivery. This preamble contains a list of the user’s friends. This is represented as an array of user ids, for example:

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

If the stringify_friend_ids=true parameter is sent, the friends list preamble will be returned as string objects (instead of integer objects). This is particularly valuable if your language or library has difficulty with 64-bit integers, because as the number of Twitter users grows, user ids will eventually exceed the 32-bit integer threshold. If the parameter is used, the friends array specified above will not be sent, and the friends_str array will be sent in its place. For example:

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

Error: Uncaught, unspecified "error" event. ([object Object]) at Stream.emit

First of all, thanks for your awesome module! I have been using it for a while now, but occasionally my app crashes with the error of 'unspecified error event'. Any idea what's causing this and how to fix this? See the following for a comprehensive error message:

Error: Uncaught, unspecified "error" event. ([object Object]) at Stream.emit (events.js:144:17) at ClientRequest. (/node_modules/user-stream/lib/user-stream.js:173:16) at emitOne (events.js:77:13) at ClientRequest.emit (events.js:169:7) at TLSSocket.socketErrorListener (_http_client.js:256:9) at emitOne (events.js:77:13) at TLSSocket.emit (events.js:169:7) at connectErrorNT (net.js:996:8) at nextTickCallbackWith2Args (node.js:442:9) at process._tickCallback (node.js:356:17)

Thanks!
Marvin

Failed to load the resource: Error has something to do with Access Control Allow Origin

I'm using react and I have the example code in my return function.

		console.log(FBPostData);
		var stream = new Stream({
		    consumer_key: 'RHs4GdqsGH7QvTioGk3G3Tsf4',
		    consumer_secret: 'zPoZrZD9YqCH51FF0R5oQU8EJOX0IMz1JbwxY64Z44dT2u4pqE',
		    access_token_key: '	140915034-ckBaxPhhILbVyIeiYvMpyuPF9dx3ZbqxL5nrPBNE',
		    access_token_secret: 'fbAAa6LxYLjCN4MCeOj5uEpQVvqeKXbrk36o3bq1IkEza'
		});

		//create stream
		stream.stream();

		//listen stream data
		stream.on('data', function(json) {
		  console.log(json);
		});
		stream.on('error', function(err) {
		  console.log(err);
		});

The error that it returns is:

OPTIONS https://userstream.twitter.com/1.1/user.json 400 (Bad Request)
ClientRequest._onFinish @ request.js:132
(anonymous) @ request.js:62
EventEmitter.emit @ events.js:78
finishMaybe @ _stream_writable.js:607
afterWrite @ _stream_writable.js:470
afterTick @ index.js:40
Item.run @ browser.js:153
drainQueue @ browser.js:123
setTimeout (async)
runTimeout @ browser.js:41
process.nextTick @ browser.js:143
nextTick @ index.js:39
onwrite @ _stream_writable.js:458
WritableState.onwrite @ _stream_writable.js:169
ClientRequest._write @ request.js:254
doWrite @ _stream_writable.js:406
writeOrBuffer @ _stream_writable.js:395
Writable.write @ _stream_writable.js:322
exports.OAuth._performSecureRequest @ oauth.js:401
exports.OAuth._putOrPost @ oauth.js:470
exports.OAuth.post @ oauth.js:479
Stream.stream @ user-stream.js:66
render @ PostVisual.js:79
(anonymous) @ ReactCompositeComponent.js:795
measureLifeCyclePerf @ ReactCompositeComponent.js:75
_renderValidatedComponentWithoutOwnerOrContext @ ReactCompositeComponent.js:794
_renderValidatedComponent @ ReactCompositeComponent.js:821
performInitialMount @ ReactCompositeComponent.js:361
mountComponent @ ReactCompositeComponent.js:257
mountComponent @ ReactReconciler.js:45
mountChildren @ ReactMultiChild.js:236
_createInitialChildren @ ReactDOMComponent.js:703
mountComponent @ ReactDOMComponent.js:522
mountComponent @ ReactReconciler.js:45
performInitialMount @ ReactCompositeComponent.js:370
mountComponent @ ReactCompositeComponent.js:257
mountComponent @ ReactReconciler.js:45
performInitialMount @ ReactCompositeComponent.js:370
mountComponent @ ReactCompositeComponent.js:257
mountComponent @ ReactReconciler.js:45
performInitialMount @ ReactCompositeComponent.js:370
mountComponent @ ReactCompositeComponent.js:257
mountComponent @ ReactReconciler.js:45
performInitialMount @ ReactCompositeComponent.js:370
performInitialMountWithErrorHandling @ ReactCompositeComponent.js:316
mountComponent @ ReactCompositeComponent.js:255
mountComponent @ ReactReconciler.js:45
mountChildren @ ReactMultiChild.js:236
_createInitialChildren @ ReactDOMComponent.js:703
mountComponent @ ReactDOMComponent.js:522
mountComponent @ ReactReconciler.js:45
performInitialMount @ ReactCompositeComponent.js:370
mountComponent @ ReactCompositeComponent.js:257
mountComponent @ ReactReconciler.js:45
mountComponentIntoNode @ ReactMount.js:104
perform @ Transaction.js:143
batchedMountComponentIntoNode @ ReactMount.js:126
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
_renderNewRootComponent @ ReactMount.js:319
_renderSubtreeIntoContainer @ ReactMount.js:401
render @ ReactMount.js:422
(anonymous) @ index.js:24
__webpack_require__ @ bootstrap c3ae3757f0d1cf6cbdb1:661
fn @ bootstrap c3ae3757f0d1cf6cbdb1:87
(anonymous) @ app.js:54243
__webpack_require__ @ bootstrap c3ae3757f0d1cf6cbdb1:661
(anonymous) @ bootstrap c3ae3757f0d1cf6cbdb1:707
(anonymous) @ bootstrap c3ae3757f0d1cf6cbdb1:707

As well as:

Twitter:1 Failed to load https://userstream.twitter.com/1.1/user.json: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

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.