How to run ratchet websocket server in parallel with apache server - php

I'm trying to create a chat application using ratchet and php. I created websocket server to listen on port 8080 in server.php file but I can't find how run it parallel to the apache server after hosting.
server.php
<?php
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Core\Socket\Chat;
use Core\Router;
use Core\Request;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
index.php
<!DOCTYPE html>
<html>
<head>
<title>A Chat App</title>
</head>
<body>
<h1>Chat App</h1>
</body>
<script>
const socket = new WebSocket('ws://localhost:8080');
socket.addEventListener('open', event => {
console.log('Connection established');
socket.send('Hey There Everyone');
});
socket.addEventListener('message', message => {
console.log(message);
});
</script>
</html>
This works fine when I run server.php using cmd and index.php on localhost. But how to run these files in parallel after hosting on the web hosting service.

You are running two different processes that want to use the same port. So this setup is not possible. But you can ask apache to serve your websocket script instead of using the command line.
See link to a post that has great details on approaches you can take:
Setting up a websocket on Apache?

Related

how to make client connect to ratchet on live server?

I've googled about this for so many days and till now the client (browsers) cannot connect to the server.
But the server can run. I think its because its connected to itself(localhost).
I did find ratchet documentation which says:
If you want to open Ratchet up (not behind a proxy) set the third
parameter of App to '0.0.0.0'.
http://socketo.me/docs/troubleshooting
so i tried this in my server.php file. (doesn't work)
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8180,
'0.0.0.0'
);
Next i tried changing the app.php file which located here(doesn't work):
/vendor/cboden/ratchet/src/Ratchet/App.php
public function __construct($httpHost = '0.0.0.0', $port = 8180, $address = '0.0.0.0', LoopInterface $loop = null) {......
Then,i tried changing the port to something else. again the server can run but the client cannot connect.
I referred all these:
How to run Ratchet remotely or on a server?
How to run Ratchet remotely or on a server?
https://github.com/ratchetphp/Ratchet/issues/394
Someone please help. All I want is for the client to be able to connect to the ratchet websocket which is running on the server.

Ratchet Websocket for raspberry pi

I have written a websocket using ratchet. The webserver is nginx and lives on a raspberry pi. The raspberry pi has been connected to the internet with a router through port forwarding. When I access the site at work, all works well. The websocket connects, all the webpages launch. When I try to access the webpage outside of work the webpage works except the websocket. The worst part is I can't even debug it because I need to be outside the local network for the websocket to fail (i.e not at work). At work, I can connect to the server using two IP addresses, the private IP address that is only accessible locally and the public IP address that can be accessed from anywhere. Both IP addresses properly launch the websocket. When I am not at work, I can only access the webserver on the Public IP address and the websocket does not work.
I have been trying to make this work for a day and a half straight now with no success. Does anybody have any suggestions? Even to help me identify the problem?
The websocket code follows the Ratchet Push Server tutorial:
<?php
require '/var/www/html/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
$context = new React\ZMQ\Context($loop);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message',array($pusher, 'onBlogEntry'));
$webSock = new React\Socket\Server($loop);
$webSock->listen(443, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\Websocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
?>
The client side code is:
var conn = new ab.Session('ws://privateIPAddress:443',
function (){
console.log("Here");
conn.subscribe('client',function(topic,data) {
console.log("hey");
...
});
},
function() {
console.warn('Websocket connection closed');
},
{'skipSubprotocolCheck': true}
);
I suspect the issue is a security setting since both the public and private IP addresses work when I am at the work site.
This is a very late answer to my own question but in case anyone is still puzzling over the same issues. The answer is don’t use ratchet. Use nodejs with socket.io. All your troubles will fly away. Socket.io which also has a java implementation is simply a more developed package for WebSockets.

PHP Ratchet can't connect from other device

I'm starting using PhP Ratchet socket. Following the guides I could made a simple chat application and it's working within the same computer. Example, if I open up chrome and firefox, I can interact send and receive messages, ok.
The problem is when I try to use the chat app from another computer, but within the same internet connection, or even when I tried to test it online.
When on lan, the other computers can't connect with the socket and online no one can connect.
By looking around, I found about using '0.0.0.0' or even port 5555 to enable connections from anyone. But even when using this, I can't connect.
How can I solve this? This is my files:
server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\http\HttpServer;
use Ratchet\WebSocket\WsServer;
require __DIR__ . '/../vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
), 2000, '0.0.0.0'
);
$server->run();
?>
And client js file:
var socket = new WebSocket('ws://127.0.0.1:2000');
127.0.0.1 will always point to the local device, which, on devices other than the socket server, will not point to the device that is hosting the socket server. If the device that is running the socket server is not routeable from the internet, you will not be able to connect to the socket from the internet.
You can test from other devices on your LAN by figuring out what your network IP is for the device that is running the server. Then in your client code, connect to that IP. It will probably be something close to 192.168.x.x, e.g. 192.168.1.12 (it could also be in the 10.x.x.x or 172.16.x.x address spaces). Then simply use that address to connect from your client script for testing:
var socket = new WebSocket('ws://192.168.1.12:2000');
You may still run into trouble if, for whatever reason, your network is configured to drop packets on port 2000. If so, it should be fairly easy for you to change to a different port for both your server and client.

How to disconnect invalid clients immediately on Ratchet PHP?

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();

Websocket: Cannot establish a connection with basic websocket server

I've tried very basic websocket tutorial using ratchet php, exactly as shown in http://socketo.me/docs/hello-world
Code for websocket server:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require 'vendor/autoload.php';
require 'chat.php';
use Ratchet\Server\IoServer;
use HHWS\Chat;
$server = IoServer::factory(
new Chat(),
8080
);
$server->run();
To run the server I did:
$ php ws-server.php
And to test the connection with the server I did:
telnet 127.0.0.1 8080
This worked perfectly fine when tested LOCALLY. Users can chat using multiple telnet terminals.
I then uploaded the code to live server. And the tried running the server.
Then tried to connect to this server using telnet just like before, it couldn't connect.
All it shows is "Trying.." message and then " Unable to connect to remote host: Connection timed out".
I don't know why this is happening, and what the problem is. The code is exactly the same. And this is very basic hello world example I'm doing. Can anyone help me on this.
Do using "Websockets" have any other requirements on the live server to work.
Update:
Actually, the live server is Amazon EC2; does this require setting up additional things for websocket to work?

Categories