简体   繁体   中英

Display twit stream on client side Meteor js

Hi everyone

I'm working on an app wich will display twwets from a certain hashtag in the browser using Meteor.js and Twit. I actually need to display a Twit stream from the server to the client side. I've actually put the stream's launch and stop in meteor methods, to activate them only when I'm in the good client view, but the console.log() only run on the server side.

Here is my code :

Server Side

    var Twit = Meteor.require('twit');
    var T = new Twit({
        consumer_key:         '...',
        consumer_secret:      '...',
        access_token:         '...',
        access_token_secret:  '...'
    });

    var stream;

    Meteor.methods({
        stream: function(hashtag) {
            //Initialisation
            stream = T.stream('statuses/filter', { track: hashtag });

            // stream launch
            stream.on('tweet', function (tweet) {

                //Console log for testing
                console.log(tweet.user.name+" a tweeté ceci: "+tweet.text+" le "+tweet.created_at);
                console.log("=========================================");

            });
        }, 
        streamStop: function() {
            stream.stop();
        }
    });

Client side

    Template.hashtagPage.rendered = function() {
        var hashtag = document.getElementById('hashtag').innerHTML;
        // Récupère les ID pour le stream
        Meteor.call('stream', hashtag);
    };

    // Stop le stream quand on quitte la page
    Template.hashtagPage.destroyed = function(){
        Meteor.call('streamStop');
    };

You can find Twit here: https://github.com/ttezel/twit

My git repo if you want: https://github.com/ABattut/croisillon/commits?author=ABattut

I've tried some solutions, like make a function who will return the T object to the client side, in vain. Return the tweet to the client and display it. I tried to use Fiber too (like seen in some questions in Google).

I don't know how to do it. I'm looking for a solution to console.log() the Twit stream on the client side, or a way to display them in the HTML directly (that would be top).

Thanks for reading and answering.

Just add tweets to a Meteor.Collection that's available on both client and server as they hit the stream, and a suitable cursor on the client side will give you the most recent tweets to display in the HTML, console or wherever.

You'll either have to add a timestamp to the tweets as you insert them, or possibly manipulate the twitterCreated field to allow you to sort by time of arrival and allow for periodic removal of old tweets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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