简体   繁体   English

Heroku,Socket.IO,Node.js

[英]Heroku, Socket.IO, Node.js

I'm setting up a node.js twitter streaming app on heroku. 我正在heroku上设置一个node.js twitter流应用程序。 It works perfectly until a second client makes a connection. 它可以完美工作,直到第二个客户端建立连接为止。 This is something I wasn't expecting until I showed it to someone :( 在向某人展示它之前,这是我没想到的:(

I am using tweet-pipe for the Twitter Stram API functionality: https://github.com/peeinears/tweet-pipe 我正在为Twitter Stram API功能使用tweet-pipe: https : //github.com/peeinears/tweet-pipe

To get Socket.IO working on Heroku I followed these instructions: https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku 为了使Socket.IO在Heroku上工作,我遵循以下说明: https ://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku

io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10); 
});

In my app.js file I have 在我的app.js文件中

io.sockets.on('connection', function (socket) {
  tp.stream('statuses/filter', params, false, function (stream) {
    stream.on('tweet', function (tweet) {
      socket.emit('tweet', tweet );
    });
  });
});

Again, this all works great until another client visits the page. 同样,这一切都很好,直到另一个客户访问该页面。 Then the original connection is lost while the second client has a connection. 然后,当第二个客户端建立连接时,原始连接将丢失。 Any thoughts? 有什么想法吗? I'm a little new to the Socket.IO stuff. 我对Socket.IO有点陌生。

AFAIK you can only have one active stream per twitter handle. 抱歉,每个Twitter句柄只能有一个活动流。 You cannot use tp.stream for second client. 您不能将tp.stream用于第二个客户端。

Sources: 资料来源:

  1. https://dev.twitter.com/docs/streaming-apis/streams/public https://dev.twitter.com/docs/streaming-apis/streams/public
  2. https://dev.twitter.com/discussions/921 https://dev.twitter.com/discussions/921

Here's how I fixed it, thanks to Chad in the comments. 感谢Chad在评论中,这是我固定的方法。 It seems to work fine now as it's live and somewhat healthy. 由于它已经存在并且还有些健康,它现在似乎可以正常工作。

io.sockets.on('connection', function (socket) {

  var tp = new TweetPipe({
    consumer_key: process.env.CONSUMER_KEY,
    consumer_secret: process.env.CONSUMER_SECRET,
    token: process.env.TOKEN,
    token_secret: process.env.TOKEN_SECRET
  });

  tp.stream('statuses/filter', params, false, function (stream) {
    stream.on('tweet', function (tweet) {
      io.sockets.emit('tweet', tweet );
    });
  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM