22

I am trying to connect to a socket.io-client using the following code:

Server:

// Load requirements
var http = require('http'),
    io = require('socket.io');

// Create server & socket
var server = http.createServer(function(req, res){

    // Send HTML headers and message
    res.writeHead(404, {'Content-Type': 'text/html'});
    res.end('<h1>Aw, snap! 404</h1>');
});
server.listen(8080);
io = io.listen(server);

// Add a connect listener
io.sockets.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

Client:

console.log('1');

// Connect to server
var io = require('socket.io-client')
var socket = io.connect('localhost:8080', {reconnect: true});

console.log('2');

// Add a connect listener
socket.on('connect', function(socket) {
    console.log('Connected!');
});

console.log('3');

I don't get the Connected console log or Client Connected console log and I don't know why! The code sample is taken from another question posted: Link and I don't see any solution to the problem...

2
  • So, what's the question? Commented Mar 18, 2015 at 1:24
  • Did you check the firewall, antivirus, other apps preventing connection? Did you run the server side code in your command line? What other steps you did to investigate? Commented Mar 18, 2015 at 1:26

4 Answers 4

21

Use the same version of socket io client and server. It will work perfectly.

Sign up to request clarification or add additional context in comments.

1 Comment

THIS! AFTER 8 HOURS OF SUFFERING! THIS IS IT! THX YOU <3!
19

Also you need to add protocol with path.

change

var socket = io.connect('localhost:8080', {reconnect: true});

to

var socket = io.connect('http://localhost:8080', {reconnect: true});

1 Comment

I tried all of these changes on my code, but the console is not logging that a connection is established on my raspberry pi when I run the script in terminal. The raspberry pi is the client.
17

Assuming you are using a socket.io version greater than 1.0, on the server, change this:

// Add a connect listener
io.sockets.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

to this:

// Add a connect listener
io.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

See the socket.io documentation reference here.


You don't want to be listening for this event only on already connected sockets. You want to listen for this event on any socket, even a newly created one.


Also, be very careful when reading socket.io code in random places on the internet. Some things changed significantly from v0.9 to v1.0 (I don't know if this was one of those things or not). You should generally always start with the socket.io documentation site first since that will always represent the latest version. Then, if looking at other internet references, make sure you only use articles that are later than mid-2014. If you don't know the vintage of an article, it's best not to rely on it without corroboration from a more recent article.

Comments

-10

you can use localhost. It works for me as well. You must use your ip address and port that works for you

1 Comment

when i changed from localhost to ipv4 addres it worked for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.