Use PHP Websocket Ratchet online

Asked

Viewed 1,395 times

0

I’m trying to use the Ratchet to create WebSockets in PHP on a site I own. When I did the tests (localhost) everything worked perfectly, but when I try to put online, it does not run the connection. Reading more on the subject, I saw that it would be necessary to change the ip to be the server one - or use the correct reference. Which led me to this final code:

var socket = new WebSocket('ws://meusite.com.br:8000');

But when I access the site, after a while, I get this message:

failed: Error in Connection establishment: net::ERR_CONNECTION_TIMED_OUT

The server file kept this code:

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

use Ratchet\Server\IoServer;
use Ratchet\http\HttpServer;
use Ratchet\WebSocket\WsServer;

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

I’m using composer also for package management Ratchet.

I don’t know what could be happening to not having the connection or what I missed, but this process is not working online, only at localhost.


Editing the question, and already adding the answer to the comment, I have access SSH yes, even when I try to start the script as done in localhost

php bin/server.php

The SSH presents the following error:

Fatal error: Class 'Ratchet http Httpserver' not found in /bin/server.php on line 10


Note: The final application will not be a chat, I’m doing so just by following a tutorial. The goal is that there is only the exchange of messages to inform that a new order has been made, or that a new service order has arrived, etc...

  • But do you have SSH access to your server to fire the application, or do you have a webapp system to set up a PHP application? websocket does not run directly on Apache or Ngnix, it is independent, it will not work if you access via http://site/script.php, unless you use the exec();, however I’m almost sure it will take privilege to start Websocket.

  • @Guilhermenascimento had forgotten this stage, I edited the question. But yes, I have access and tried to boot as done on localhost, but still it presents an error.

  • Spun the composer update? Tell me one thing it wouldn’t be right require __DIR__ .'/vendor/autoload.php';? Places the folder structure

  • @Guilhermenascimento I believe require is as it is, because from the root I have 2 folders bin/server.php and vendor/autoload.php. Within the vendor there are also other folders such as Ratchet, Composer, etc.. But I didn’t ask Poser update, and when I tried it now, it presented an error stating that there was no permission or it was not in a directory that I have permission for (something like this)

1 answer

1


Change:

use Ratchet\http\HttpServer;

For:

use Ratchet\Http\HttpServer;

Because this occurs:

In Windows the files are case-insentive, when you run:

use Ratchet\http\HttpServer;

It usually finds this in Windows (see that Http begins with the letter H capitalized):

./src/Ratchet/Http/HttpServer.php

Already Like-Unix systems such as linux use the file system as case-sensitive, then at the time you use use Ratchet\http\HttpServer; he seeks this:

./src/Ratchet/http/HttpServer.php

But on the server the file is like this:

./src/Ratchet/Http/HttpServer.php

Are different.

  • Show. A part solved. I managed to run the SSH and did not show any error.. However the connection is not yet established. After a certain time he returns to present the error of time out that I commented on the question.

  • @Did Celsomtrindade release port 8000? Maybe you don’t have to run via SSH but set up on Dashboard, what kind of server do you use? Would AWS be?

  • Yeah, I don’t remember doing anything to clear the door... Did you mean which hosting service I use? That’s it?

  • @Celsomtrindade this, which hosting service, because depending on there is no way to release. Now if you have "total" control, as it is in AWS.

  • Currently using www.weblink.com.br

  • @Celsomtrindade Nossa :D ... is this server good? Does it fall a lot? Is Mysql stable? Dude I was surprised unlimited email accounts... About yours.

  • Dude, I run a website agency and host my clients there. I’ve never had any complaints, never had a problem with support, nothing like that.. Whenever I terminated my services, they were always online. hahahaha On the question, I am already in contact with them to verify what may be happening, because by trying connection on 80 or 443 me denied access and any other port of time out error.

  • @Celsomtrindade Port 80 and 443 is from Apache, there is no way to run a socket in them, they are already in use. Besides that even if you inactivate Apache and run the script still might not work.

  • Yes, they are the only ones that return some error. All the others run the script, but do not connect, always points out the error. Any tips or suggested alternatives?

  • So it’s like I said, Websocket is one thing, Http sites is another, there’s no way to mix both. Now there are two exits, the first is to change service, but if you read the doc of Ratchet you may be able to use it without websocket, see this example: http://socketo.me/docs/wamp

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.