0

I am creating a Node web app using Express.js and twitter.js. I am sucsessfully recieving the twitter stream on my server, however, I am unable to stream that data to my client(AJAX call). When attempting to do so I always recieve this error on the second tweet :

Error: Can't set headers after they are sent.

I know what causes this error (res.send calls res.end), but I am not sure how to get around it using Express.

HERE IS MY EXPRESS SERVER

app.get('/gamepage/twitter', function(req, res, next){
client.stream('statuses/filter.json', params, function(stream){
    stream.on('data', function(event){
        console.log(event);
        //game module calls
        mix.getTweets(event.user.name, event.text); //Store tweet
        mix.getLastPrice(); // set the last price
        let last = mix.lastPrice;
        console.log('last price: '+ last);
        //mix.trackScores(mix.tweets.user); // look for cmd and change score accordingly

        // send object
        res.send({
            allTweets: mix.tweets,
            price: mix.price,
            scores: mix.scores
        });

    });
    stream.on('error', function(error){
        throw error;
    });

HERE IS MY AJAX

function getTweets () {

let xhrTweets = new XMLHttpRequest();
xhrTweets.open('GET', 'http://localhost:3003/gamepage/twitter', true);
xhrTweets.addEventListener('load', function(){
    console.log('loaded');
    //getPrice();
    let data = JSON.parse(xhrTweets.response);
    let mostRecent = 0//data.allTweets.length-1;
    console.log(data);
    console.log(data.allTweets);
    console.log(data.price);
    console.log(data.scores);

    let oldContainer = document.getElementById('gameresults');
    let newContainer = document.createElement('div');
    newContainer.setAttribute('id', 'gameresults');
    document.getElementById('resultcontainer').replaceChild(newContainer, oldContainer);

    let resultDisplay = document.createElement('div');
    resultDisplay.setAttribute('class', 'tweetContainer');
    let newTweet = document.createElement('p');
    let tweetUser = document.createElement('p');
    let newPrice = document.createElement('p');
    let displayText = document.createTextNode(data.allTweets[mostRecent].text);
    let displayUser = document.createTextNode(data.allTweets[mostRecent].user);
    let displayPrice = document.createTextNode(data.price);

    newContainer.appendChild(resultDisplay);
    resultDisplay.appendChild(tweetUser);
    resultDisplay.appendChild(newTweet);
    resultDisplay.appendChild(newPrice);
    newTweet.appendChild(displayText);
    tweetUser.appendChild(displayUser);
    newPrice.appendChild(displayPrice);

});

//xhrTweets.timeout = 15*1000;
//xhrTweets.addEventListener('timeout', function(data){
//  console.log('timeout');
//  console.error(data);

//});

xhrTweets.send();

}

So how do I stream data from twitter Through express and into my AJAX call while keeping the stream open for later data?

Riley Hughes
  • 1,344
  • 2
  • 12
  • 22

1 Answers1

0

The problem is that you try to do res.send multiple times, every time a chunk of data is received from the stream. Check out this SO question I think it can help you

  • I understand whats causing the error but I still don't understand the solution, I saw similar answers like this before, but how does this solve my problem if res.end is called anyway? I need the stream to stay open to continuously receive tweets as they are made – Riley Hughes Dec 01 '17 at 20:31
  • As I expected when finding a similar answer before, This answer gives me this error `Error: write after end ` – Riley Hughes Dec 01 '17 at 20:37