1

Iam building an application that listens to an internal connection of a certain external url and insert its internally loaded content to a new page at my end.

My statement above might sound confusing but a good analogue of the external url are Instagram stories when you visit this external site, it loads complete in the browser (the rolling icon stops, but this doesn't stop the request of unseen updates )

I have built a JavaScript server using nodejs running on npm and have set the content header

Am also using htaccess to make it stay alive so that I can listen and load new internal connection

Header set Connection keep-alive>

but doesn't seems to work

    if(err){

        if(err == 'ENOENT'){
            res.writeHead(404, {'Content-Type' : 'text/html'});
            res.write(path.join(__dirname, 'public', '404.html'));
            res.end();

        }else{
            res.writeHead(500, {'Content-Type' : 'text/html'});
            res.write('<enter><h1>Some server error just occured .... </h1></center>');
            res.end();
        }

    }else{
        res.writeHead(200, {'Content-Type' : contentType, 'Connection':' keep-alive', 'Transfer-Encoding':' chunked', 'Accept':' */*', 'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'});
        res.write(content);
        res.end();
    }

Am expecting to get the new loaded content each time a connection runs underground without reloading the browser and again just like scrolling through Instagram status / stories

1
  • If your server is running nodejs, why did you tag PHP ? Commented Aug 23, 2019 at 18:10

1 Answer 1

1

I think this is the wrong approach. You could just have it check every 5 seconds to see if there is something new on the client side.

function checkURL(){
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.write(this.responseText);
        }
    };
    xhttp.open("GET", "https://example.com/", true);
    xhttp.send();
}
setInterval(checkURL,5000); //5000 = 5 seconds

If you must do it in Node.JS, take a look at this post: HTTP keep-alive in node.js

Sign up to request clarification or add additional context in comments.

5 Comments

I have done exactly that also but it loads just the first page of the url and then ends the connection
Can you give a little more context of your project?
when you visit instagram.com/stories/@username/ it loads the first page with the first connection (if you are using the xmlhttprequest to fetch the content it returns a page which has just the site logo) then stops and loads the rest through what I believe to be xmlhttprequest
not all but it only returns the content from the first established connection
How much data is the content?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.