I'm stumped. I've looked how many times I've looked at similar questions. Yes, they exist, and they have a solution.
But I don't understand what to do there at all. I don't understand the answers. Because I'm a beginner.
I have php7.2, Ubuntu 22.04, apache2.
I have done everything according to Ratchet Hello World documentation;
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
920
);
$server->run();
The server itself works if you connect: var conn = new WebSocket("ws://(ip):920");.
How do I connect SSL?
I don't know where to put, for example, ProxyPass /wss2/ ws://(ip):920/. And I don't know the rules for that line.
Did I create the server correctly?
Where is httpd.conf, because I have it nowhere in /etc/apache2/.
enter image description here
Please help in any way, any help is welcome. I did not know that I would take so long to create a server Ratchet. I have been sitting and studying for 4 days, I don't know what to do or where to go anymore.
UPD:
My apache configuration path is like this: etc/apache2/apache2.conf
Related
hello all im try to make chat as real time im used websocket with
php ratchet packege and this my code : in my file server.php this
code
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/rat/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8000
);
$server->run();
also my secend file is a script js in message.php :
var conn = new WebSocket('ws://localhost:8000');
conn.onopen = function(e) {
console.log("Connection established!");
};
please what i should to do w://loclahost:8000 is correct ? or
should i changed in my host name ? or domain name? or what
secend qution what i should use port
i had searched in google but i find more article that telk about httpd i cant f
ind my answers please
I've set up ReactPHP as a WebSocket listener so I can send out near-realtime updates to subscribed browsers. It looks like this:
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Listener;
$rootPath = realpath(__DIR__ . '/..');
require_once $rootPath . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Listener()
)
),
8081
);
$server->run();
All of those classes except for Listener are part of Ratchet, and the Listener is my handler implementation of \Ratchet\MessageComponentInterface, which responds to the open/close/error events of connecting WebSockets.
I would like this script to be contactable by a queue system I am building, and since that is a system channel, I would like to expose this on a different port so that it is not reachable from the internet. This would ideally be on HTTP rather than WebSocket; I have successfully used a PHP WebSocket client to contact this listener, but that's a bit complicated compared to a file_get_contents('http://...') call!
At a guess, the internal loop of React (e.g. StreamSelectLoop::streamSelect) only blocks for short time (see here) so it feels possible that it could manage a number of separate streams internally. Ideally, I would like the additional port to be handled by a separate listener, for security isolation (in case it is possible for a flaw in Guzzle to allow an attacker to appear to come from the restricted port when in fact they came from the internet port).
I think this is not an unusual requirement, but there does not seem to be any related information in the docs, and the GitHub issues are rather bare here too. Is this configuration possible without setting up parallel processes or multithreading?
One solution to this problem is to add IP whitelisting in the WebSocket listener for specific message types. I may do that in the short term, but listening on another port would be much nicer solution.
I found one way, it may be not a best practice, but you can manually add socket, like this:
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Listener;
use React\Socket\Server as Reactor;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Listener()
)
),
8081 //Port 1
);
$socket = new Reactor($server->loop);
$socket->listen(8082, '0.0.0.0'); //Port 2
$socket->on('connection', [$server, 'handleConnect']);
$server->run();
Everything works fine on my local machine on XAMPP. But after I uploaded the files to a server, it gives a error like this...
Fatal error: Uncaught exception 'React\Socket\ConnectionException' with message 'Could not bind to tcp://0.0.0.0:8080: Address already in use' in
/home/sites/jemaottest.com/public_html/websocket/vendor/react/socket/src/Server.php:29 Stack trace: #0
/home/sites/jemaottest.com/public_html/websocket/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(70): React\Socket\Server->listen(8080, '0.0.0.0') #1
/home/sites/jemaottest.com/public_html/websocket/bin/chat-server.php(17): Ratchet\Server\IoServer::factory(Object(Ratchet\Http\HttpServer), 8080, '0.0.0.0') #2 {main} thrown in
/home/sites/jemaottest.com/public_html/websocket/vendor/react/socket/src/Server.php on line 29
when I run the chat-server.php file.
I found out something on the troubleshooting page of Ratchet which says,
If you want to open Ratchet up (not behind a proxy) set the third parameter of App to '0.0.0.0'.
I tried that but it didn't work,
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__).'/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080,
'0.0.0.0'
);
$server->run();
?>
it still gave the same error.
What should I do now?
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8282
);
Just Change the port and try.. mine is works fine after changing my port. And also don't forget to change the port in your port in websocket javascript class too.
var conn = new WebSocket('ws://yourdomain.com:8282');
I later found out using other ports are not allowed on a shared server.
If you are on a private server with a ssh access, you can try MarshallOfSound's solution.
Or if you just need the websocket as a service you can use something like Pusher.
It means that nother process is running on port 8080. Probably a webserver of some kind.
You can find out what is running with the command
lsof -i :8000
You can either stop the process already using the port. Or run Ratchet on a different port.
I started learning Ratchet PHP. I did the first tutorial on Ratchet website (http://socketo.me/docs/hello-world) which basically just opens a server and sends incoming text messages to other clients.
I send messages with javascript console in Firefox and It works as expexted.
Meanwhile, without any specific reason I connect to Ratchet server with telnet localhost 8080. For sure it does nothing as the server expects WS protocol.
At this point, it is obivous that telnet is an invalid client for my app. So I don't want it to hang around and user my server's resources.
What can I do for this purpose? Is there an option or parameter I can specify to disconnect this connection or can I detect it easily with serverside code and disconnect it from there with some kind of fingerprint/agent or with a timeout?
Server code:
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
I was trying to deploy my websocket server and start running it but always gives:
PHP Fatal error:
Uncaught exception 'React\Socket\ConnectionException'
with message 'Could not bind to tcp://my_ip_here:8080:
Address already in use'
in /var/www/html/webscoket/vendor/react/socket/src/Server.php:29
here's my server.php:
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\Socket\Server;
use React\ZMQ\Context;
$loop = React\EventLoop\Factory::create();
$app = new onyxsocket();
$webSock = new Server($loop);
$webSock->listen(8080, 'my_ip_here');
$webServer = new IoServer(
new HttpServer(
new WsServer(
$app
)
),
$webSock
);
$context = new Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://my_ip_here:5555');
$pull->on('error', function ($e) {
var_dump($e->getMessage());
});
$pull->on('message', array($app, 'onbroadcast'));
$loop->run();
What I've tried so far is to check available ports that can be used in the production server: netstat - anp gave me idea that port 8080 is free. But the problem is it still show the error Address already in use. I also tried other ports given by the administrator but no luck.
The server.php that I'm trying to deploy is working fine on localhost. But I don't know what do I need to do to make it work on a production server.
Need help. Thanks.
from #user3666197 comment above:
Clarify the code, pls. Your server-code ZeroMQ .bind() -s to port# 5555. So whose code binds to localhost port# 8080, that is reported in an unhandled exception above? How do you clean for a gracefull-exit any crashed EventLoop/Factory to release resources and avoid hanging orphans?
I decided to recheck the netstat: netstat -tulpen and check for the ports 8080 and 5555 and find out that there is registered PID on the port currently connecting. These application was also the same script that I want to run on console, server.php.
I kill the PID: kill PID_number and run the server.php on console again. It worked.