Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7583303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:38:41+00:00 2026-05-30T18:38:41+00:00

My goal is to update the web page with some file. When I edit

  • 0

My goal is to update the web page with some file. When I edit and save the file, and instantly its content appears updated to a page.

I´m using socket.io to handle this issue. But. I need some modifications on my code to approaches it.

:: My server side code:

var PORT = 8001;
var io = require("socket.io").listen(PORT);
var fs = require("fs");

io.sockets.on('connection', function(socket) {
    console.log("Connected!");
    socket.emit('connected', { accept: true});

    console.log("Trying to send a content file to client...");
    fs.readFile("file.txt","UTF-8", function(err, data) {
        if (err) throw err;
        socket.emit("requestFile", data );
        console.log("Conteúdo:", data);
    })
});

console.log("Application has started! Port: " + PORT);

:: My page

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="http://localhost:8001/socket.io/socket.io.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style>
            .arquivo {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 14px;
                width: 900px;
                border: 2px solid black;
                -moz-border-radius: 3px;
            }
        </style>
        <script type="text/javascript">
            console.log("Try to logon...");
            var socket = io.connect("localhost", {"port" : "8001"});

            socket.on("connected", function(data) {
                console.log("Connected User?", data.accept);
            });

            //recebe o arquivo indefinidamente
            var requestFile = socket.on("requestFile", function(data) {
                $("#arquivoSaida").html(data + "<br/>");
                console.log(data);
            });

            setInterval(requestFile, 200);
        </script>
    </head>
    <body>
        <h3 style="font: Arial, Verdana; font-size: 14px; font-weight: bold;">
            File updated:
        </h3>
        <div id="arquivoSaida">

        </div>      
    </body>
</html>

I have used a setInterval to fires at 200 ms to request a file to a server. Any cue will be very welcome.

Thanks in advance!

— UPDATED with Solution:

:: I did some alterations from my original code. This solution was provided by PuerkitoBio. Thanks, man!

The code updated:

var PORT = 8001;
var io = require("socket.io").listen(PORT);
var fs = require("fs");

io.sockets.on('connection', function(socket) {
    console.log("Connected!");
    socket.emit('connected', { accept: true});

    console.log("Trying to send the content to a client...");
    console.log("dir", __dirname);

    fs.watch(__dirname + "/arquivo.txt", function(event, filename) {
        console.log("Event:", event);

        if (event == "change") {
            fs.readFile("arquivo.txt","UTF-8", function(err, data) {
                if (err) throw err;
                socket.emit("receiveFile", data );
                console.log("Content:", data);
            })
        }

    });

});

console.log("Application has started! Port: " + PORT);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:38:42+00:00Added an answer on May 30, 2026 at 6:38 pm

    You can use fs.watchFile() and compare curr.mtime with prev.mtime to detect if the file has been modified. Then use readFile to send the content to your connected client. From the docs.

    With sockets, you don’t poll the data from the client, it gets pushed from the server when required (when your file changes, in your case). So you don’t need the setInterval from the client side (you will watch for changes server-side using fs.watchFile, and push the content to your client, so your “requestFile” handler on the client will get called automatically (should be renamed receiveFile or something to be more accurate!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to connect to an OWA page (Microsoft Office Outlook Web Access
[UPDATE] the clean goal runs smoothely all over the projects of the solution, the
Goal is to make a dialog that appears on menu_key pressed, but it keeps
I'm working through Agile Web Development with Rails, Edition 4 with some tweaks (mostly
I'm using Rails with jQuery, and I'm working on a page for a simple
I am using SignalR in an ASP.Net Web Application project and am having issues.
We developed an offline HTML5 web-app using the .manifest caching mechanism. Everything works as
My goal is to synchronize a web-application with an internal database. The web-application has
I have a SOAP web service and I'm trying to figure how to save/log
my goal is to implement a message system on web site ( i mean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.