PHP WebSocket on DigitalOcean App Platform

168 views Asked by At

I'm trying to make a websocket server on DigitalOcean App Platform using PHP as back-end socket server.

The purpose is to use websocket for a real time multiplayer game, using Java for the client App and PHP for the back-end one. Now I'm first developing the back-end part.

I tried this code locally and it works.

composer.json:

{
    "name": "my/testratchet",
    "require": {
        "cboden/ratchet": "0.4.4"
    }
}

startServer.php:

require_once('MySocketServer.php');
use TestWebSockets\MySocketServer;

require __DIR__ . '/vendor/autoload.php';

$DEFAULT_URL  = 'localhost';

$DEFAULT_PORT = 8080;

// Run the server application through the WebSocket protocol on port 8080
$app = new Ratchet\App($DEFAULT_URL, $DEFAULT_PORT);
$app->route('/chat', new MySocketServer(), array('*'));
$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
$app->run();

MySocketServer.php:

namespace TestWebSockets;

require __DIR__ . '/vendor/autoload.php';

use Exception;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;

class MySocketServer implements MessageComponentInterface
{
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        echo "Received data: $msg\n";
        foreach ($this->clients as $client) {
            if ($from != $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        $conn->close();
    }
}

testClientWeb.php

<script>
    var conn = new WebSocket('ws://localhost:8080/echo');
    conn.onmessage = function(e) { console.log(e.data); };
    conn.onopen = function(e) { conn.send('Hello Me!'); };
</script>

To test it:

  • from terminal:

    • cd into the project root dir

    • composer install

    • php startServer.php

      It remains running without any error

  • from browser:

    • open the console with F12

    • go to network tab

    • in the url type http://localhost/projectDir/testClientWeb.php

    • in the console a network row appears: click on it and go to the innertab Response. You'll see:

      • ↑ Hello Me! (green arrow: sent)
      • ↓ Hello Me! (red arrow: received)

Now, before deploy it to digital ocean, I replaced "localhost" with "my-generated-project-url.ondigitalocean.app" and "ws" with "wss" because as required by DigitalOcean, in App Platform there is only one opened port, the 8080, and SSL is forced, so I must use wss to connect to the server socket.

Then, on digital ocean I created an App on App Platform with these settings:

  • source: my repository
  • only one component as 'Web Service'
  • Source Directory: /
  • autodeploy: on
  • 0 environment variables
  • only one route: /
  • Health Checks: TCP
  • Build Command: none
  • Run Command: heroku-php-apache2 && php startServer.php
  • HTTP Port: 8080

After the App deploy is finished, this is the output of "Runtime Logs" on digital ocean:

Detected
Detected 512MiB of RAM
PHP memory_limit is 128M Bytes
Starting php-fpm with 4 workers...
Starting httpd...
Application ready for connections on port 8080.

When I browse the client page (https://my-generated-project-url.ondigitalocean.app/testClientWeb.php), the output in "Runtime Logs" on digital ocean is:

"GET /testClientWeb.php HTTP/1.1" 200 295 "-" "Mozilla...

In the browser's network inspection, this error occurs (my Firefox is already up to date, without any add-on):

enter image description here

I tried the example (with node js) illustrated in this official post and it works: https://docs.digitalocean.com/developer-center/deploy-an-app-using-websockets-to-app-platform/ but I need to use PHP as backend.

How to make it works with PHP?

0

There are 0 answers