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 9256359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:55:17+00:00 2026-06-18T11:55:17+00:00

I am making a simple node.js app and intend to use socket.io but so

  • 0

I am making a simple node.js app and intend to use socket.io but so far I cannot get the server to start.

This is my code:

var http = require('http'),
    io = require('socket.io'),
    fs = require('fs');

http.createServer(function(request, response){
    fs.readFile(__dirname + '/index.html', function(err, data){
        if(err){
            response.writeHead(500, {'Content-Type': 'text/plain'});
            return response.end('Error');
        }
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.end(data);
    });
}).listen(1337);

io.sockets.on('connection', function(socket){
    socket.emit('pic', { addr: '/pic.jpg' });
    socket.on('status', function(data){
        console.log(data);
    })
})

and this is the output i receive:

[root@ip-10-224-55-226 node]# node server.js

/srv/node/server.js:16
io.sockets.on('connection', function(socket){
           ^
TypeError: Cannot call method 'on' of undefined
    at Object.<anonymous> (/srv/node/server.js:16:12)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
[root@ip-10-224-55-226 node]#

Socket.IO was installed by NPM and seems to reside in /root/node_modules/socket.io
Because of that I have made a sym-link (ln -s) of that directory to /srv/node where my server root is.

  • 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-06-18T11:55:18+00:00Added an answer on June 18, 2026 at 11:55 am

    io is just a library. You want to listen to connect to one specific instance:

    var http = require('http'),
        io = require('socket.io'),
        fs = require('fs');
    
    var serv = http.createServer(function(request, response){
        fs.readFile(__dirname + '/index.html', function(err, data){
            if(err){
                response.writeHead(500, {'Content-Type': 'text/plain'});
                return response.end('Error');
            }
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.end(data);
        });
    });
    serv.listen(1337);
    
    // Bind socket.io to server
    var serv_io = io.listen(serv);
    serv_io.sockets.on('connection', function(socket){
        socket.emit('pic', { addr: '/pic.jpg' });
        socket.on('status', function(data){
            console.log(data);
        })
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple server in node.js using the TLS module. So far so
I'm starting with node.js and am making a simple MVC framework. So far, I've
I'm making a simple URL request with code like this: URL url = new
I'm making a simple app using the HTML5 <canvas> tag and compile the code
Background I'm making a simple web server using express.js on top of node.js. When
I am following a tutorial on making a simple app with Node.js and Geddy.
I'm implementing a little aunthentication routes on my Node app, as simple as this:
So basically I'm making a simple RTS board game with node.js/express/socket.io for a learning
I'm making a simple script reads out the nginx server status in node.js I
in making a simple cgi server for a course. To do that in some

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.