Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why node.js requires an upgrade while trying to run an application on the localhost?

When I try to run my node.js application on a localhost server, it does not run and demands a required upgrade. I have tried to run the code but I get the following error:

screenshot

server code

 var WebSocketServer = require('ws').Server,
    ws = new WebSocketServer({port: 80}),
    CLIENTS=[];


**new connection etablished**
 ws.on('connection', function(conn) {
         CLIENTS.push(conn);
         conn.on('message', function(message) {
         console.log('received:  %s', message);
         sendAll(message);

    });


 console.log("new connection");
         conn.send("NOUVEAU CLIENT CONNECTE");

                **if you close connection**
         conn.on('close', function() {
           console.log("connection closed");
           CLIENTS.splice(CLIENTS.indexOf(conn), 1);
         });

    });
    **send messeages vers all clients**

function sendAll (message) {
    for (var i=0; i<CLIENTS.length; i++) {
      var j=i+1;
      CLIENTS[i].send("Message pour le client "+j+": "+message);
    }
}

client code

      <p>
        Result :<output name="" type="text" id="result" value"readonly"></output>
      </p>
      <input type="text" onchange="ws.send(this.value);">
      </body>
      <script>
          var ws =new WebSocket('ws://localhost:80');
          ws.onmessage=function(event){
              document.getElementById("result").value=event.data;
          }
      </script>
like image 348
riadh zar Avatar asked Feb 29 '16 11:02

riadh zar


People also ask

Why node JS Cannot be used for CPU intensive applications?

The single-threaded implementation makes Node a bad choice for CPU-intensive programs. When a time-consuming task is running in the program it blocks the event loop from moving forward for a longer period.

Why do you need node JS to run JavaScript?

Node. js is easily employed as a server-side proxy where it can handle a large amount of simultaneous connections in a non-blocking manner. It's especially useful for proxying different services with different response times, or collecting data from multiple source points.

For which applications Nodejs is not well suited for?

Not Suitable for Heavy-Computing Apps Node. js doesn't support multi-threaded programming yet. It is able to serve way more complicated applications than Ruby, but it's not suitable for performing long-running calculations. Heavy computations block the incoming requests, which can lead to decrease of performance .


2 Answers

Upgrade Required is a reference to the header that is sent when establishing a WebSocket connection between a client (i.e. the browser) and the server.

Like @Prinzhorn stated in his comment, you need a client application that connects to your WebSockets server, which could be a static html page. I recommend you reading this introduction to websockets to understand better how WebSockets work.

like image 125
mauricioschneider Avatar answered Oct 17 '22 10:10

mauricioschneider


Do not open a client HTML file as a localhost URL but open the file directly.

After running your web-socket server,

localhost:[port]/client.html -> you will get the message "upgrade required".

file:///[folder]/client.html -> you can see your HTML file.

because you don't have any web-server with a web-socket or you did not configure your web server for your web-socket. So, you should use your file system.

The easiest way is to use right click on the client file and open it with your favorite browser.

like image 29
fairisle Avatar answered Oct 17 '22 08:10

fairisle