Re: Chat example and dependencies

2,312 views
Skip to first unread message

cboden

unread,
Dec 11, 2012, 9:22:12 AM12/11/12
to ratch...@googlegroups.com, gis...@gmail.com
In your chat-server.php you must include the following line at the top of your script:

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

On Tuesday, 11 December 2012 08:38:32 UTC-5, gis...@gmail.com wrote:
Hello everyone,

I'm trying to run the Chat example but php is not able to resolve the needed libraries. I have installed composer and I have created the composer.json file. Composer then downloads the libraries and place them under the folder vendor. But when I run:

php chat-server.php

I get: PHP Fatal error:  Interface 'Ratchet\MessageComponentInterface' not found in /home/luca/tmp/f3w/src/MyApp/chat.php on line 6

I think that there must be something wrong with my project structure, the folder/file structure is the following:

-src
--chat-server.php
--composer.json
--MyApp\chat.php

To run composer I use the following command: php /home/luca/bin/composer.phar install
What am I doing wrong?

Thanks in advance

cboden

unread,
Dec 11, 2012, 1:24:57 PM12/11/12
to ratch...@googlegroups.com, gis...@gmail.com
This is because composer doesn't know how to load your "MyApp" namespace.  Your Chat.php (case sensitive) class should be in a folder (commonly named) /src (/src/MyApp/Chat.php).  Update your composer.json file to the following and then re-run composer.phar install:

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/Ratchet": "0.2.*"
    }
}

On Tuesday, 11 December 2012 12:52:01 UTC-5, gis...@gmail.com wrote:
This is how it looks my chat-server.php. I forgot to mention that I even had to add require_once otherwise it would not find my chat.php class

<?php
require_once("MyApp/chat.php");
use Ratchet\Server\IoServer;
use MyApp\Chat;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new Chat()
      , 8080
    );

    $server->run();

cboden

unread,
Dec 11, 2012, 3:26:43 PM12/11/12
to ratch...@googlegroups.com, gis...@gmail.com
I think I see your issue - it's your directory structure.  I lay mine out like this:

-bin
--chat-server.php
-composer.json
-src
--MyApp
----Chat.php
-vendor
--autoload.php

On Tuesday, 11 December 2012 14:57:28 UTC-5, gis...@gmail.com wrote:
I have changed chat.php to Chat.php, I have the same composer.json, and I have tried to re-run the composer but I still get the same error:

PHP Fatal error:  Interface 'Ratchet\MessageComponentInterface' not found in /home/luca/tmp/f3w/src/MyApp/Chat.php on line 6

cboden

unread,
Dec 11, 2012, 8:34:16 PM12/11/12
to ratch...@googlegroups.com, gis...@gmail.com
You need to wrap your class in a WsServer instance to speak the WebSocket protocol.

On Tuesday, 11 December 2012 17:45:05 UTC-5, gis...@gmail.com wrote:
Thanks with this new structure it works.

However, I managed to test it only with telnet because with Javascript WebSocket it is not connecting. Is there a need of a special javascript library to connect?

cboden

unread,
Dec 12, 2012, 4:45:26 PM12/12/12
to ratch...@googlegroups.com, gis...@gmail.com, gis...@gmail.com
You're PHP build is missing the cURL extension.

On Wednesday, 12 December 2012 16:43:27 UTC-5, gis...@gmail.com wrote:
You are right, I forgot about it. However there is still a problem, when I try to connect with the html/javascript page the chat-server.php die with:

PHP Fatal error:  Call to undefined function Guzzle\Http\Curl\curl_version() in /home/luca/tmp/f3w/websocket/vendor/guzzle/http/Guzzle/Http/Curl/CurlVersion.php on line 47

Any idea?

cboden

unread,
May 11, 2013, 9:57:52 AM5/11/13
to ratch...@googlegroups.com, gis...@gmail.com, machadok...@gmail.com
There's a problem with your autoloader:

* You forgot to set MyApp in your composer.json file
* You forgot to include the composer autoloader
* Your Chat class is in the wrong spot
* Your Chat class is named incorrectly inside its file

On Wednesday, 8 May 2013 13:02:08 UTC-4, machadok...@gmail.com wrote:
I've the same directory structure that you write but I've this error :

PHP Fatal error:  Class 'MyApp\Chat' not found in /home/djnivek/projets/hpFam07/bin/chat-server.php on line 9

doskaskon...@gmail.com

unread,
Jun 16, 2013, 12:48:04 PM6/16/13
to ratch...@googlegroups.com, gis...@gmail.com, machadok...@gmail.com, riti...@gmail.com
Exactly...json wasn't up-to-date...thanks

On Friday, May 17, 2013 8:00:42 AM UTC+3, riti...@gmail.com wrote:
I had the same issue after following the install steps from http://socketo.me/docs/install
and then changed composer.json as shown in http://socketo.me/docs/hello-world
i had to 
1.  php /usr/local/bin/composer.phar install --dev
this gave an error saying that composer.lock file had to be update
2. php /usr/local/bin/composer.phar list
to get help 
3. php /usr/local/bin/composer.phar update
4. php /usr/local/bin/composer.phar validate

then it works.
So its seems like composer.lock file was causing probs

gilberts...@gmail.com

unread,
Aug 7, 2013, 8:07:18 PM8/7/13
to ratch...@googlegroups.com, gis...@gmail.com, machadok...@gmail.com, riti...@gmail.com
riti thanks so much. Step 3 was what I needed after the hello world page instructed new content for the composer.json file.


On Friday, 17 May 2013 15:00:42 UTC+10, riti...@gmail.com wrote:
I had the same issue after following the install steps from http://socketo.me/docs/install
and then changed composer.json as shown in http://socketo.me/docs/hello-world
i had to 
1.  php /usr/local/bin/composer.phar install --dev
this gave an error saying that composer.lock file had to be update
2. php /usr/local/bin/composer.phar list
to get help 
3. php /usr/local/bin/composer.phar update
4. php /usr/local/bin/composer.phar validate

then it works.
So its seems like composer.lock file was causing probs


On Saturday, 11 May 2013 19:27:52 UTC+5:30, cboden wrote:

van.a...@gmail.com

unread,
Sep 13, 2013, 8:38:17 AM9/13/13
to ratch...@googlegroups.com, gis...@gmail.com
Why don't you put this structure on the website ?
Think of the time we save.
Look at all the messages you receive asking for the same thing.

When you want to test a library you need a quick test script so you can decide if you'll go with it or not.

Sander Mendes

unread,
Jan 21, 2015, 8:09:34 PM1/21/15
to ratch...@googlegroups.com, gis...@gmail.com
Hello guys

I'm trying too run "php bin/chat-server.php",

I have the structure folder:

public_html/stream/
-bin
--chat-server.php
-src
--MyApp
----Chat.php
-vendor
--autoload.php
-composer.json
-composer.lock

when i try to run this command # php bin/chat-server.php is not work and i get the error message:
"PHP Fatal error:  Class 'MyApp\Chat' not found in /home/server/public_html/stream/bin/chat-server.php on line 13".

I'm using Centos 6.5.

What am I doing wrong?

Any help will be appreciated.

Thank's.

cboden

unread,
Jan 25, 2015, 10:58:11 AM1/25/15
to ratch...@googlegroups.com, gis...@gmail.com
Sander, make sure you have your MyApp namespace in composer.json, run `composer update`, and make sure you're including the autoload file in your chat-server.php file. 
Reply all
Reply to author
Forward
0 new messages