Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect scheduled when using Twit inside a class #302

Open
conradoplg opened this issue Oct 15, 2016 · 3 comments
Open

Reconnect scheduled when using Twit inside a class #302

conradoplg opened this issue Oct 15, 2016 · 3 comments

Comments

@conradoplg
Copy link
Contributor

This is a very weird behaviour. See this example (seems artificial, but it's the minimal working example):

function User(consumer_key, consumer_secret, token, secret) {
    this.twit = new Twit({
        consumer_key: consumer_key,
        consumer_secret: consumer_secret,
        access_token: token,
        access_token_secret: secret,
        timeout_ms: 5 * 1000,
    })
}

var user = new User(secret["consumer_key"], secret["consumer_secret"], secret["access_token"], secret["access_token_secret"])

var stream = user.twit.stream('user')
stream.on('message', function (tweet) {
  console.log('message: ' + JSON.stringify(tweet, null, 4))
})

When it runs, it connects to the stream but after a few seconds, it closes the connection and schedules a reconnect. Eventually I get throttled by Twitter. The reconnect is called by this stacktrace (I got it by adding console.trace('scheduling reconnect for ' + self._connectInterval) to StreamingAPIConnection.prototype._scheduleReconnect):

Trace: scheduling reconnect for 0
    at StreamingAPIConnection._scheduleReconnect (/home/conrado/workspace/quiriquiri/node_modules/twit/lib/streaming-api-connection.js:269:11)
    at StreamingAPIConnection._onClose (/home/conrado/workspace/quiriquiri/node_modules/twit/lib/streaming-api-connection.js:174:8)
    at emitNone (events.js:67:13)
    at Request.emit (events.js:166:7)
    at Gunzip.<anonymous> (/home/conrado/workspace/quiriquiri/node_modules/twit/node_modules/request/request.js:974:51)
    at emitNone (events.js:72:20)
    at Gunzip.emit (events.js:166:7)
    at emitCloseNT (zlib.js:471:8)
    at nextTickCallbackWith1Arg (node.js:430:9)
    at process._tickCallback (node.js:352:17)

However, if I change the code to this:

var T = new Twit({
    consumer_key: secret["consumer_key"],
    consumer_secret: secret["consumer_secret"],
    access_token: secret["access_token"],
    access_token_secret: secret["access_token_secret"],
    timeout_ms: 60 * 1000, // optional HTTP request timeout to apply to all requests.
})

var stream = T.stream('user')

stream.on('message', function (tweet) {
  console.log('message: ' + JSON.stringify(tweet, null, 4))
})

It works! It seems it only happens when I instantiate Twit inside a class. (If I instantiate it outside the class and put it in the class attribute, it also works, e.g. user.twit = T)

Does anyone have any idea what's going on?

@ioRekz
Copy link

ioRekz commented Oct 21, 2016

You also changed the timeout_ms in your examples

@conradoplg
Copy link
Contributor Author

@ioRekz wow, that's embarassing. I was so baffled that I missed the obvious. Thanks!

However, shouldn't Twit ignore the timeout for streams? I'll open a PR.

@andreapozzetti
Copy link

same problem for me @conradoplg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants