phpsocket.iocomposer-phprequirephp-socket

PHP fatal error: class not found (Composer not installing all classes?)


I want to use phpsocket.io, but the installation process is very poorly described and I'm stuck at an annoying issue. I installed the package with Getcomposer.org but not all the required classes were installed. I tried to test the basic chat demo provided:

use Workerman\Worker;
use PHPSocketIO\SocketIO;

// listen port 2021 for socket.io client
$io = new SocketIO(2021);
$io->on('connection', function($socket)use($io){
  $socket->on('chat message', function($msg)use($io){
    $io->emit('chat message', $msg);
  });
});

Worker::runAll();

The Workerman\Worker class was installed successfully, but the PHPSocketIO/SocketIO class gives me an error:

Fatal error: Uncaught Error: Class 'PHPSocketIO\SocketIO' not found in /my/root/directory/public_html/PHPSocketIO/server.php:7
Stack trace:
#0 {main}
  thrown in /my/root/directory/public_html/PHPSocketIO/server.php on line 7

How am I supposed to access that class? It wasn't installed with Getcomposer and I can't find the class folder anywhere. Also, like I said, the install instructions are very poorly described on the Github page. It says under 'Install':

composer require workerman/phpsocket.io

What does this mean? What am I supposed to do with this information? The section under 'Run chat example' is also very vague. I tried to cd to examples/chat but there's no server.php file to run. Is this page outdated, or am I just missing something here? All I know it's not very beginners-friendly, so if someone can give me a push in the right direction of where I'm supposed to get the PHPSocketIO\SocketIO class, it would be much appreciated. Thank you!


Solution

  • Add this line before your code. This set a namespace of third party.

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

    I recommended use sokect.io of nodejs.