not sending to client

I am trying to create a simple script to send data from a file every to the client every time the file is updated. I have tested and found that the file is read, but the client doesn’t receive anything. there are no errors in the console. I am fairly new to

node.js code

var app = require('express')();
var http = require('http').Server(app);
var io = require('')(http);
var fs = require("fs");
var port = process.env.PORT || 3000;

app.get('/', function(req, res){
    res.sendFile(__dirname + '/popup.html');

fs.watchFile("assets/popup.json", {interval:100}, function(curr, prev)
    fs.readFile("assets/popup.json",{encoding:"utf8"}, function(err, data){
        io.emit("popup", data)
http.listen(port, function(){
    console.log('listening on *:' + port);

client code

var socket = io();
socket.on('popup', function(msg){


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Whenever things aren’t working right like this, you need to resort to “debug mode”. In that mode, you need to gather all possible events that might be happening and see what you learn from that. To that end, add this code to the client:

var socket = io();
socket.on('popup', function(msg){
    console.log("hello: ", msg)
socket.on('connection', function() {
    console.log("client connected");

socket.on('connect_error', function(err) {
    console.log("client connect_error: ", err);

socket.on('connect_timeout', function(err) {
    console.log("client connect_timeout: ", err);

These messages are all documented in the client-side doc on the Github site which you can find by Googling “ github” at any time.

Then, see what you see in the browser console when the page loads. If you don’t know how to open the browser console in whichever browser you are using, google it to find out. You need to be looking at the debug console when the page loads.

FYI, we’re assuming that you’ve loaded into the page via a script tag before this code. If not, that error will show in the console too.

The OP then gets this error:

client connect_error: 
    Error: server error at Socket.onPacket ( 
      at XHR.<anonymous> ( 
      at XHR.Emitter.emit ( 
      at XHR.Transport.onPacket ( 
      at callback ( 
      at Object.exports.decodePayload ( 
      at XHR.Polling.onData ( 
      at Request.<anonymous> ( 
      at Request.Emitter.emit ( 
      at Request.onData (

OK, progress. How are you loading in the client page? This seems like it might be that you have mismatched versions of in client and server. You should be doing:

<script src="/"></script>

and then your server will be feeding the client page the exact same version of Also, since this error reports client-side 1.2.0, what version of is installed on the server?

Method 2

try this

    socket.on('popup', function(msg){

Method 3

The issue appears to be you don’t actually connect to a local server. By running node server.js with the code below you can start a web server. Then navigate to localhost in your browser to see the changes in console made to popup.json.


var app = require('http').createServer(handler);
var io = require('')(app);
var fs = require('fs');


function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
        function (err, data) {
            if (err) {
                return res.end('Error loading index.html');


fs.watchFile("popup.json", {interval: 100}, function (curr, prev) {
    fs.readFile("popup.json", {encoding: "utf8"}, function (err, data) {
        io.emit("popup", JSON.parse(data));


<script src="/"></script>
    var socket = io('http://localhost');
    socket.on('popup', function (data) {

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x