PTN
PTN

Reputation: 1692

Socket io on connection not connecting NodeJS

I just started learning NodeJS and I am trying to make a simple server-client project using Socket io.

What happens right now is that when I open localhost:8001, I don't see any logs inside the listener.sockets.on block.

var http = require('http');
var app = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('<h1>Hello!</h1>');
}).listen(8001);

var io = require('socket.io');
var listener = io.listen(app);

console.log("Sample Socket IO");

listener.sockets.on('connection', function (socket) {
    console.log('a user connected');
    socket.emit('connected', 'Welcome');

    socket.on('disconnect', function () {
        console.log('user disconnected');
    });
});

Upvotes: 0

Views: 44

Answers (2)

Jason Livesay
Jason Livesay

Reputation: 6377

The browser page needs to load the socket.io client code for one thing. That is the first thing missing that I can see. Look through the example here http://socket.io/get-started/chat/ and make sure you are following exactly at first and then make changes after you get that example working. Your server code looks a bit different from their example also.

Upvotes: 0

eoD .J
eoD .J

Reputation: 312

It looks like the logging will occur when a connection happens. You setup a listening socket, so try to connect to it. Try 'telnet 127.0.0.1 8001' to connect.

Upvotes: 1

Related Questions