Question

I am getting following error while implementing danielnill example

when i added connection action and client side code in this example i got this error.

Error:

/home/oneadmin/Desktop/test-projects/nodejs/basic-example/server.js:40
io.sockets.on('connection', function(socket){
          ^
    TypeError: Cannot read property 'on' of undefined
        at Object.<anonymous> (/home/oneadmin/Desktop/test-projects/nodejs/basic-example/server.js:40:11)
        at Module._compile (module.js:449:26)
        at Object.Module._extensions..js (module.js:467:10)
        at Module.load (module.js:349:32)
        at Function.Module._load (module.js:305:12)
        at Function.Module.runMain (module.js:490:10)
        at startup (node.js:124:16)
        at node.js:807:3

server.js

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

var server = http.createServer(function(request, response){
    console.log('Connection');
    var path = url.parse(request.url).pathname;

    switch(path){
        case '/':
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write('hello world');
            response.end();
            break;
        case '/socket.html':
            fs.readFile(__dirname + path, function(error, data){
                if (error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                }
                response.end();
            });
            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404");
            response.end();
            break;
    }

});

server.listen(8001);
io.listen(server);
io.sockets.on('connection', function(socket){
    socket.emit('message', {'message': 'hello world'});
});

socket.html

<html>
    <head>
        <script src="/socket.io/socket.io.js"> </script>
    </head>
    <body>
        <script>
            var socket = io.connect();

            socket.on('message', function(data){
                console.log(data.message);
            });

        </script>

    <div> This is our socket.html file</div>

    </body>
</html>

Pls help me to understand error and how do i solve it?

Was it helpful?

Solution

You'll want to capture the result of socket.io's .listen() to setup server-side listeners:

server.listen(8001);

var ios = io.listen(server);
ios.sockets.on('connection', function(socket){
    socket.emit('message', {'message': 'hello world'});
});

The sockets object isn't a member of the socket.io module itself, but of a listening instance of io.Manager().


In the post you linked, there's a typo in one of the snippets (under "Adding Socket.io"):

// ...

var io.listen(server);

I'd assume that was meant to be:

var io = io.listen(server);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top