Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luk's avatar
Level 1

I faced this "Interface 'Ratchet\MessageComponentInterface' not found" problem

I want to use Ratchet web socket in my project and when i tray to connect it i face this problem and i tray to update and dump the composer but it dose not work ! any one who can help me please ? here is my composer.json file

{
    "autoload": {
        "psr-4": {
            "MyChatApp\": "src"
        }
    },
    "require": {
        "cboden/ratchet": "^0.4.1"
    }
}

my server.php file

<?php
require 'core/Chat.php';
require __DIR__ . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use MyApp\Chat;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;

$server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    new Chat()
                )
            ),
          8081
         );$server->run();

my chat.php file

<?php
namespace MyChatApp;
  
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface
 {  protected $clients;

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

   public function onOpen(ConnectionInterface $conn)
  {
     // Store the new connection to send messages to later
     $this->clients->attach($conn);
     echo "New connection! ({$conn->resourceId})\n";
  }  

  public function onMessage(ConnectionInterface $from, $msg) 
  {
      $numRecv = count($this->clients) - 1;
      echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n",

        $from->resourceId, $msg, $numRecv, $numRecv ==  1 ? '' : 's');    
        foreach ($this->clients as $client) {
           if ($from !== $client) {
        //The sender is not the receiver, send to other clients
          $client->send($msg);
        }
      }
  }

    public function onClose(ConnectionInterface $conn)
  {
     // The connection is closed, remove from connection list
     $this->clients->detach($conn);
     echo "Connection {$conn->resourceId} has disconnected\n";
  }

    public function onError(ConnectionInterface $conn, \Exception $e)
  {
      echo "An error has occurred: {$e->getMessage()}\n";
      $conn->close();
  }
}

and the error is "Fatal error: Interface 'Ratchet\MessageComponentInterface' not found in C"

0 likes
5 replies
ramonrietdijk's avatar

You should include your composer dependencies first in server.php.

<?php

require __DIR__ . '/vendor/autoload.php'; // this one on top
require 'core/Chat.php';
1 like
luk's avatar
Level 1

@Sinnbeck Ofcourse it is for learning purpose I did that and anther error come "Fatal error: Uncaught Error: Class 'MyApp\Chat' not found in C:\wamp64\www\insa\bin\server.php:13"

ramonrietdijk's avatar

@habte The error is thrown because the namespace is incorrect. Update your server.php again:

<?php
// change
use MyApp\Chat;
// to
use MyChatApp\Chat;
1 like
luk's avatar
Level 1

@ramonrietdijk i updated and my 8080 port is used by another and i want to use 8081 port and i face this error : "Fatal error: Uncaught RuntimeException: Failed to listen on "tcp://0.0.0.0:8081": An attempt was made to access a socket in a way forbidden by its access permissions. (EACCES) in C:\wamp64\www\insa\bin\vendor\react\socket\src\TcpServer.php:184"

Please or to participate in this conversation.