Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: io is not defined [closed]

I know this is an error talked about a lot in here. But after a few posts found via google it still didn't solve my problem.

I get the above error when trying to include the io javascript in my index.html.

I've tried: <script src="http://localhost:8080/socket.io/socket.io.js"></script> And <script src="/socket.io/socket.io.js"></script>

And even tried switching the port to 3000. The problem still persists nonetheless.

I've tried restarting my app.js and reinstalling socket.io(Although it is in my package) with npm install socket.io. I see that a socket.io folder is created in my node_modules folder.

I start my server as following:

var express = require('express');

var app = express(), 
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

app.listen(8080);

And my index also loads the latest jQuery from Google: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

My terminal shows that socket.io is started when i restart my app.js:

23 Jun 13:31:56 - [nodemon] starting `node app.js`
   info  - socket.io started

My index.html in total(I retyped/remodeled it from a tutorial: http://psitsmike.com/2011/09/node-js-and-socket-io-chat-tutorial/)

<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<script>

    var socket = io.connect();

    socket.on('connect', function() {
        socket.emit('adduser', prompt("Hoe heet je?"));
    });

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

    socket.on('updateUsers', function(data) {
        $('#users').empty();
        $.each(data, function(k, v) {
            $("#users").append('<div>' + k + '</div>');
        });
    });

    $(document).ready(function() {
        $('#datasend').click(function() {
            //get the message and empty the input
            var msg = $('#data').val();
            $('#data').val('');

            //Let the server execute sendchat along with the msg
            socket.emit("sendChat", msg);
        });

        //ENTER key
        $('#data').keypress(function(e) {
            keyCode = (e.keyCode ? e.keyCode : e.which);
            if (keycode == 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>
like image 362
CaptainCarl Avatar asked Dec 02 '22 22:12

CaptainCarl


1 Answers

In your code, change this

app.listen(8080);

to this

server.listen(8080);
like image 143
user568109 Avatar answered Dec 17 '22 12:12

user568109