'WebSocket connection to 'ws://..' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

Pulling my hair searching from last 4 hours this simple code for socket connection and getting error. I have changed port as well but nothing works.

 //create a new WebSocket object.
        var wsUri = "ws://localhost:9000/demo/server.php";  
        websocket = new WebSocket(wsUri);

    websocket.onopen = function(ev) { // connection is open 
        $('#message_box').append("<div class=\"system_msg\">Connected!</div>"); //notify user
    }

    $('#send-btn').click(function(){ //use clicks message send button   
        var mymessage = $('#message').val(); //get message text
        var myname = $('#name').val(); //get user name

        if(myname == ""){ //empty name?
            alert("Enter your Name please!");
            return;
        }
        if(mymessage == ""){ //emtpy message?
            alert("Enter Some message Please!");
            return;
        }

        //prepare json data
        var msg = {
        message: mymessage,
        name: myname,
        color : '<?php echo $colours[$user_colour]; ?>'
        };
        //convert and send data to server
        websocket.send(JSON.stringify(msg));
    });

Error :

WebSocket connection to 'ws://localhost:9000/demo/server.php' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

while the PHP is

$host = 'localhost'; //host
$port = '9000'; //port
$null = NULL; //null var

//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

//bind socket to specified host
socket_bind($socket, 0, $port);

//listen to port
socket_listen($socket);

//create & add listning socket to the list
$clients = array($socket);


Solution 1:[1]

You're not creating a websocket server. This is what you need: http://socketo.me/

Getting websockets to work properly in PHP is annoying and a mess (especially, if you're a beginner). You might be over your head.

Solution 2:[2]

Your PHP uses a regular (raw) tcp/ip socket connection. Websockets is a protocol with a specific handshake and packet framing... if the server doesn't speak "Websockets", the client will raise an error.

P.S.

PHP was originally designed to act as a web app scripting language for HTTP connections, usually using CGI to quickly return a String that acts as the file/html to be sent.

By nature, PHP processes are intended to have shorter life-spans.

I would not recommend using PHP to write a websocket server, although you could look into Ratchet which is a PHP websocket library that could help you to build a websocket app using PHP.

I recommend you consider Ruby, node.js or Python for your Websocket project - it will save you a lot of headaches as you dig deeper into your project and you are likely to have better community support for any challenges you face along the way.

Solution 3:[3]

Try to switch off all antivirus the maybe blocking sockets port for protection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrius
Solution 2 Community
Solution 3 Themba Ngubeni