0

I have just made a chat-room using socket.io and jquery. I was unsure about how to get it online so I just uploaded the files to an old ftp I had. (I am really new with node.js) Loading up the website was normal, but when I looked in the console there was an error

Uncaught ReferenceError: io is not defined
    at HTMLDocument.<anonymous> ((index):55)
    at j (jquery-latest.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-latest.min.js:2)
    at Function.ready (jquery-latest.min.js:2)
    at HTMLDocument.J (jquery-latest.min.js:2)
(anonymous) @ (index):55
j @ jquery-latest.min.js:2
fireWith @ jquery-latest.min.js:2
ready @ jquery-latest.min.js:2
J @ jquery-latest.min.js:2

The error is pointing to the <script> tag in the html code (the first variable set is where the error is):

<script>
    $(function(){
        var socket = io.connect();
        var $messageForm = $('#messageForm');
        var $message = $('#message');
        var $chat = $('#chat');
        var $messageArea = $('#messageArea');
        var $userFormArea = $('#userFormArea');
        var $userForm = $('#userForm');
        var $users = $('#users');
        var $username = $('#username');

        $messageForm.submit(function(e){
            e.preventDefault();
            socket.emit('send message', $message.val());
            $message.val('');
        });

        socket.on('new message', function(data){
            $chat.append('<div class="well"><strong>'+data.user+'</strong>: '+data.msg+'</div>');
        });

        $userForm.submit(function(e){
            e.preventDefault();
            socket.emit('new user', $username.val(), function(data){
                if(data){
                    $userFormArea.hide();
                    $messageArea.show();
                }
            });
            $username.val('');
        });

        socket.on('get users', function(data){
            var html = '';
            for(var i = 0;i < data.length;i++){
                html += '<li class="list-group-item">'+data[i]+'</li>';
            }
            $users.html(html);
        });
    });
</script>

I am wanting to put this website online and run it with the socket.io and node.js as it's part of a larger project I am working on. If I am doing it completely wrong, I am sorry. Can you guys help me fix this error or tell me what I'm doing wrong please.
Many Thanks.

1 Answer 1

1

You need to import socket.io library before your script definition:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.