Sunday, 19 March 2017

Using socket.io to listen for events that are happening in the controller

I'm having trouble getting the back-end to emit data to the front-end using websockets. Part of the code in my controller right now consists of a function in which near the end this portion fo the code is written:

Edited: This whole function is called through ajax from another controller

exports.twitterStream = function(filter){

if (stream !== null){
    stream.stop();
    console.log('Stream stop and restarting with new filter');
}

stream = T.stream('statuses/filter', { track: filter });

//When twitterStream init, start streaming
stream.on('tweet', function (tweet) {

    if(filter !== []){

        //Exclude all tweets with undefined hashtag
        //filter all hashtags out and concat #
        if (tweet.entities.hashtags !== undefined){
            var hashtagFilterNormalised = tweet.entities.hashtags.map(function(hashtag){
                return '#' + hashtag.text.toLowerCase();
            })  

            //create tweet objects
            var newTweet = new Tweet();
            var tweetDate = tweet.created_at.substring(4,10);
            newTweet.created = tweetDate;
            newTweet.text = tweet.text;
            newTweet.tag = hashtagFilterNormalised;

            //check with matching hashtag name in mongodb => returns an ARRAY of all matching hashtags
            Hashtag.find( {'tag': { $in: hashtagFilterNormalised } }, function(err, matchingHashtag){

                if (err){
                    return console.log(err);
                }

                //loop through all matching hashtags
                matchingHashtag.forEach(function(hashtag){

                    //push tweets to matching hashtag
                    hashtag.tweets.push(newTweet);

                    //save incoming tweet to DB
                    hashtag.save(function(err, hashtag){
                    if(err){
                        return console.log(err);
                    }
                    console.log('new tweet saved to DB');

                    //socket.tweetSaved(newTweet);

                    })
                })
            })
        }
    }
})

}

So my question is, how can I get my back-end socket.js file to listen for an incoming event from the above controller file, say at the time when I save my hashtag to my database collection? My socket.js file code:

module.exports = function(io){
console.log('server side socket connected');

// Add a connect listener
io.on('connection', function(socket){

//Push count of tweets when a new tweet is saved
**Event listener here to fire when hashtag.save invoked in controller**
    socket.emit('newTweet', function(confirm){
        //emit new tweet object to client-side
    })

I've searched through some other answers elsewhere but I can only find answers that tell me to wrap the controller as a socket listener. However, my controller already consists of one whole function that is being exported and is called via ajax elsewhere, so I would not want to touch it too much if possible. Any help would be greatly appreciated.



via Andy Ho

No comments:

Post a Comment