Skip to content

socket.io problem (io is undefined) #393

Closed
viter opened this Issue Jul 16, 2012 · 12 comments

2 participants

@viter
viter commented Jul 16, 2012

Hello.
I have simple socket.io project that works well on my local machine but has problems on nodester.
I installed socket.io with the command "nodester npm install ch1 socket.io".
I changed port to that given me by "nodester app info ch1"
My server app seems to run well and its log shows:

New PID: 23206
chroot_runner
chroot_runner
Spawing /app/server.js
Running node v-0.8.1
info: socket.io started

But the client can't load socket.io.js and it says "io is undefined".
Here is part of my clent:
script src="/socket.io/socket.io.js"
var socket = io.connect('http://ch1.nodester.com:80');

Firebug says "http://ch1.nodester.com/socket.io/socket.io.js 404 Not Found"

What can be the problem?

@alejandro
Collaborator

Interesting, I'm getting the same error localy with node-v0.8.x. I don't know if socket.io is fully supported on this version. Try to downgrade. Maybe that's the problem.

@viter
viter commented Jul 16, 2012

I changed my package.jason to "node": "0.6.17"
My app log is now:
New PID: 16545
chroot_runner
chroot_runner
Spawing /app/server.js
Running node v-0.6.17
info: socket.io started

But the problem is not fixed.

@alejandro
Collaborator

Well, that's weird. Are you connecting correctly to the app?

I tested this and it worked:

var app = require('express').createServer()
  , sio = require('socket.io');

app.listen(8100);

var io = sio.listen(app);

app.get('/', function(req, res){
    res.writeHeader(200,{'Content-type':'text/html'});
    res.end('<script src="/socket.io/socket.io.js"></script>');
});

io.sockets.on('connect', function(dt,cb){
    console.log(arguments);
});

Are you doing something different?

@viter
viter commented Jul 16, 2012

It's really weird, because it works on my local machine but fails to work on nodester. It just can't find socket.io.js on client side.

@alejandro
Collaborator

Yeah! Another workaround is to put all the socket.io logic above the listen event of the main app.

var app = require('express').createServer()
  , sio = require('socket.io');

app.get('/', function(req, res){
    res.writeHeader(200,{'Content-type':'text/html'});
    res.end('<script src="/socket.io/socket.io.js"></script>');
});

app.listen(8100, function(){;
  var io = sio.listen(app);


  io.sockets.on('connect', function(dt,cb){
    console.log(arguments);
  });
})

Also you can reinstall socket.io, maybe your local version is not working properly.

@viter
viter commented Jul 16, 2012

Here is my code
server.js

var app = require('express').createServer()
 , sio = require('socket.io');

app.listen(19763);
var io = sio.listen(app);
// routing
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

var usernames = {};
io.sockets.on('connection', function (socket) {
    socket.on('sendchat', function (data) {
        io.sockets.emit('updatechat', socket.username, data);
    });

    socket.on('adduser', function(username){
        socket.username = username;
        usernames[username] = username;
        socket.emit('updatechat', 'SERVER', 'you have connected');
        socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
        io.sockets.emit('updateusers', usernames);
    });

    socket.on('disconnect', function(){
        delete usernames[socket.username];
        io.sockets.emit('updateusers', usernames);
        socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
    });
});

index.html

<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
    var socket = io.connect('http://ch1.nodester.com:80');
    socket.on('connect', function(){
        socket.emit('adduser', prompt("What's your name?"));
    });

    socket.on('updatechat', function (username, data) {
        $('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
    });

    socket.on('updateusers', function(data) {
        $('#users').empty();
        $.each(data, function(key, value) {
            $('#users').append('<div>' + key + '</div>');
        });
    });

    $(function(){
        $('#datasend').click( function() {
            var message = $('#data').val();
            $('#data').val('');
            socket.emit('sendchat', message);
        });

        $('#data').keypress(function(e) {
            if(e.which == 13) {
                $(this).blur();
                $('#datasend').focus().click();
            }
        });
    });

</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
    <b>USERS</b>
    <div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
    <div id="conversation"></div>
    <input id="data" style="width:200px;" />
    <input type="button" id="datasend" value="send" />
</div>

And again i works perfect on my local computer, but fails on nodester.

@viter
viter commented Jul 16, 2012

When I was installing the socket.io module I got this warning:

nodester npm WARN excluding symbolic link lib/socket.io-client.js -> io.js

What is this? Can it cause the problem?

@alejandro
Collaborator

Usually that happen. Not sure if that's the problem, though.

I tried to run your code locally, and it gave me the same error. I ran:

npm install express socket.io

Which installed express@3.0.0beta7 and socket.io@0.9.6. So that express version breaks socket.io. Try to uninstall express and only after you have uninstalled express, run:

nodester npm install {appname} express@2.5.x

And let's see if that works.

BTW. Update your nodester-cli to be able to run that command ([sudo] npm install -g nodester-cli)

@viter
viter commented Jul 16, 2012

I reinstalled express already. My express version now is 2.5.11 just like on my local machine. But problem still remains :(

@alejandro
Collaborator

But I'm getting the file -> http://ch1.nodester.com/socket.io/socket.io.js are you sure?

@viter
viter commented Jul 16, 2012

You are right. Now it works.
Looks like to use socket.io@0.9.6 we must install express at least 2.5.11 but not 3.0.0beta7. And it works great with node 0.8.1.

Thank you very much.

@alejandro
Collaborator

No problem. Hack the planet \m/

@alejandro alejandro closed this Jul 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.