2

I have my node.js and socket.io setup and running perfectly on my local machine, but now I am trying to transfer it to my live server.

I have installed node.js and socket.io and they're working, but I can't seem to link to the socket.io/socket.io.js file through my client. It keeps coming back with "500 Internal Server Error"

I have tried using both these paths:

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

and

<script type="text/javascript" src="https://localhost:8080/socket.io/socket.io.js"></script>

Why isn't it being found?

Sneaksta
  • 1,041
  • 4
  • 24
  • 46

4 Answers4

0

"500 Internal Server Error" usually means "server crash" or at least "server encountered an exception". So the problem might not a missing socket.io.js.

Regardless, when I have a discrepancy between local working and remote not working, it's sometimes due to a difference in the environment variables. Where are you deploying your node.js? Heroku, EC2, Joyent?

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
  • I'm not sure I'm experienced enough to know what you mean regarding deploying my node.js. I have installed it on a server and and running the server.js file through ssh. – Sneaksta Sep 13 '12 at 04:15
0

Have you changed the connection-string? Have you checked with the browser inspector, if the javascript-file is loaded?

var sio = io.connect('http://yourdomain.com:80');
sio.socket.on('error', function (reason){
  console.error('Unable to connect Socket.IO', reason);
});
Patrick
  • 7,903
  • 11
  • 52
  • 87
  • This doesn't work, because it's not even finding the socket.io.js file. I dont understand why not. Do I have to install socket.io in a specific location perhaps? – Sneaksta Sep 13 '12 at 13:26
  • Nope. `` I'm using the same link and it works fine for me. Check your server side, re-install socket.io. Especially the 500 is strange, should be 404 or 200. – Patrick Sep 13 '12 at 21:14
  • Yes I'm getting a 404 now. Not sure what was with the 500. Not sure if you noticed, but I'm trying to use the HTTPS protocol by the way. Could this be the problem? If I use HTTP, it seems to find the file, I think, but then firefox says that it's insecure. – Sneaksta Sep 13 '12 at 23:25
  • I'm actually getting this: "GET https://localhost:8000/socket.io/1/?t=1347579646863" which comes up in red in the FF console. – Sneaksta Sep 13 '12 at 23:41
  • Did you check this: http://stackoverflow.com/questions/6599470/node-js-socket-io-with-ssl? Seems to be related. – Patrick Sep 17 '12 at 13:48
  • Yes I did thanks Patrick. I have just figured out how to get it working without SSL (just using http), which wasn't even working before. So now it's just a matter of connecting via SSL. Is there anything I need to do different other than replace 'http' with 'https' on the client and server? – Sneaksta Sep 17 '12 at 14:18
  • Not that I know of. What was the problem, for future reference? – Patrick Sep 17 '12 at 15:22
0

Here i post two files one is chat.js and other is chat.html . This has the path for socket.io.js in html.this works.

1) chat.js :

var io = require("socket.io");
var socket = io.listen(1223);
socket.set("log level", 1);
var people = {};
socket.on("connection", function (client) {
     client.on("join", function(name){
    people[client.id] = name;
    client.emit("update", "You have connected to the server.");
    socket.sockets.emit("update", name + " has joined the server.")
    socket.sockets.emit("update-people", people);
});
client.on("send", function(msg){
    socket.sockets.emit("chat", people[client.id], msg);
});
client.on("disconnect", function(){
    socket.sockets.emit("update", people[client.id] + " has left the server.");
    delete people[client.id];
    socket.sockets.emit("update-people", people);
});

});

2) chat.html :

     <!DOCTYPE html>
       <html lang="en">
       <head>
        <script src="http://localhost:1223/socket.io/socket.io.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">    </script>
        <script>
        $(document).ready(function(){
            var socket = io.connect("127.0.0.1:1223");
            $("#chat").hide();
            $("#name").focus();
            $("form").submit(function(event){
                  event.preventDefault();
            });
            $("#join").click(function(){
                var name = $("#name").val();
                if (name != "") {
                    socket.emit("join", name);
                    $("#login").detach();
                    $("#chat").show();
                    $("#msg").focus();
                    ready = true;
                }
            });
            $("#name").keypress(function(e){
                if(e.which == 13) {
                    var name = $("#name").val();
                    if (name != "") {
                        socket.emit("join", name);
                        ready = true;
                        $("#login").detach();
                        $("#chat").show();
                        $("#msg").focus();
                    }
                }
            });
            socket.on("update", function(msg) {
                if(ready)
                    $("#msgs").append("<li>" + msg + "</li>");
            })
            socket.on("update-people", function(people){
                if(ready) {
                    $("#people").empty();
                    $.each(people, function(clientid, name) {
                        $('#people').append("<li>" + name + "</li>");
                    });
                }
            });
            socket.on("chat", function(who, msg){
                if(ready) {
                    $("#msgs").append("<li><strong><span class='text-success'>" + who + "</span></strong> says: " + msg + "</li>");
                }
            });

            socket.on("disconnect", function(){
                $("#msgs").append("<li><strong><span class='text-warning'>The server is not available</span></strong></li>");
                $("#msg").attr("disabled", "disabled");
                $("#send").attr("disabled", "disabled");
            });
            $("#send").click(function(){
                var msg = $("#msg").val();
                socket.emit("send", msg);
                $("#msg").val("");
            });
            $("#msg").keypress(function(e){
                if(e.which == 13) {
                    var msg = $("#msg").val();
                    socket.emit("send", msg);
                    $("#msg").val("");
                }
            });

        });
    </script>
   </head>
   <body>
    <div class="row">
      <div class="span2">
        <ul id="people" class="unstyled"></ul>
      </div>
      <div class="span4">
        <ul id="msgs" class="unstyled"></ul>
      </div>
    </div>
    <div class="row">
       <div class="span5 offset2" id="login">
        <form class="form-inline">
        <input type="text" class="input-small" placeholder="Your name" id="name">
            <input type="button" name="join" id="join" value="Join" class="btn btn-primary">
        </form>
    </div>
    <div class="span5 offset2" id="chat">
      <form id="2" class="form-inline">
        <input type="text" class="input" placeholder="Your message" id="msg">
        <input type="button" name="send" id="send" value="Send" class="btn btn-success">
      </form>
        </div>
      </div>
   </body>
</html>

Run chat.js using command - node chat.js and run chat.html in browser.

user1570577
  • 134
  • 1
  • 6
0

I know it's been 8 years old but I faced the same error, maybe my explanation could help someone.

By "trying to transfer to my live server" I did deploying it to virtual shared hosting that is running Node through Passenger module of Apache. Then I contacted the tech support and they said the script is starting to listen some ports and crashes and it is simply not possible on this type of hosting plan, I should apply for VPS/VDS instead.

It sounds strange because the app did not even start to listen, it's just about accessing static files. But probably the way Express is delivering static files is not working. Logs say:

[pid 66771] 19:33:18 listen(12, 511) = -1 EPERM (Operation not permitted) events.js:183 throw er; // Unhandled 'error' event ^

I was able to read Express is using "stream" (nodejs.org/api/stream.html) to deliver static files and I have a suggestion it is simply not working on that type of hosting. The other static files are existing physically so they are delivered with Nginx and they do not fail. This makes some surprise as some files are loaded and some give error 500, not even 4xx when the resource can't be located.

Basically response 500 tells us the output unexpectedly ended. The logs say "end of script before headers". It is unpleasant when you can't access any log messages and just receive response 500 and have to contact support to see the logs.

Andrey Putilov
  • 148
  • 4
  • 13