Игорь
Игорь

Reputation: 1

PHP websockets doesn't works on wss://

I moved my site to https://. On http to sockets there was a connection through ws: //sitename.com: 3003, now it is necessary that they were available on wss: //sitename.com: 3003. How do I do this? Thank you.

PHP:

$loop   = React\EventLoop\Factory::create();
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:3004');
$pull->on('message', array($pusher, 'onMessage'));

$webSock = new React\Socket\Server($loop);
$webSock->listen(3003, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(
  new Ratchet\Http\HttpServer(
    new Ratchet\WebSocket\WsServer(
      new Ratchet\Wamp\WampServer(
        $pusher
      )
    )
  ),
  $webSock
);

$loop->run();

JS:

window.phpSocket = new ab.Session('wss://sitename.com:3003',

nginx:

server {
    listen   443 ssl;
    keepalive_timeout   70;

    client_max_body_size    500M;
    root /var/www/path/to/site/root;
    index index.php;
    server_name sitename.com;
    gzip_static on;
    gzip            on;
    gzip_min_length 1000;
    gzip_proxied    expired no-cache no-store private auth;
    gzip_types      text/plain application/xml text/css text/javascript application/javascript;


    ssl                     on;
    ssl_certificate         /etc/letsencrypt/live/sitename.com/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/sitename.com/privkey.pem;
    ssl_session_cache       shared:SSL:10m;
    ssl_session_timeout     10m;

Upvotes: 0

Views: 607

Answers (1)

GuangshengZuo
GuangshengZuo

Reputation: 4687

Because your nginx conf does not specify how to forward your client wss to your server port listened at 3003.

you should add this to your server block in nginx.conf

location /wss/ {
    proxy_pass http://localhost:3003;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

And change your Js code to this:

window.phpSocket = new ab.Session('wss://sitename.com/wss/',

More info about how to use nginx as a websocket_proxy, please refer to https://www.nginx.com/blog/websocket-nginx/

Upvotes: 1

Related Questions