PHP WebSocket doesn't connect - php

I'm using Ratchet to create a PHP WebSocket connection. On my localhost it's working normally and as expected, but when I upload it to my server it just stops working.
I'm using an online shared hosting and this is the code on the PHP file:
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
), 8000, '0.0.0.0'
);
And this is my JS file
var socket = new WebSocket('ws://mywebsite.com:8000');
The only error I got is this:
Error in connection establishment: net::ERR_CONNECTION_REFUSED
Note: I've already ran php server.php to initiate the ws server via SSH command. I got no errors there but still can't connect. I also tried using different port but nothing seems to be working.
Why I'm trying to use WebSockets?
I have an e-commerce and I'm using the WebSockets to load/update each order without needing to refresh the page and also without using JavaScript Interval, so if an order change from 'Packing..' to 'Delivery' status, the system autoupdate without needing to refresh the page.
If there is another way around this, I'm open to ideas, as long as I can get the final result.

Related

How to create a tunnel to a remote AWS database after being connected in ssh to the Bastion host, all using in PHP

I am trying to create a tunnel using ssh2_tunnel() to a remote AWS database, after being connected in ssh to the bastion host, via php, that has access to this remote MySql db.
Here is my actual code :
<?php
$ssh = ssh2_connect('domainofbastionhost', 22);
if (ssh2_auth_password($ssh,'myuserofthebastion','mypassofthebastion'){
$tunnel = ssh2_tunnel($ssh,'xxxx.xxx.xxx.rds.amazonaws.com',3306);
$stream_set_blocking($tunnel,true);
//Everything works fine up to here
$db = new PDO('mysql:host=xxxx.xxx.xxx.rds.amazonaws.com;port=3306;dbname=myawsdbname','myusertoaccessmyawsdb','mypasstoaccessmyawsdb');
}
?>
Due to this $db line, I get on my localhost page "Uncaught PDOException: SQLSTATE[HY000] [2002] Connection timed out ....".
I have tried to connect to the db from the bastion host (by ssh) through my shell, everything works fine, I am greeted by the "Trying 'IPOFTHEAWSDB'... Connected to 'canonical domain name of the db'. Escape character is '^]'. ] 5.5.5-xx.x.xx-MariaDB-xxxxxxmysql_native_password", which means everything works fine. I even tried to connect directly to the aws db, and I get disconnected, which means that my bastion host is clearly whitelisted to access the db.
I think the problem is that when I want to create my new PDO object, I am not whitelisted to access the resource, which means I have to use that $tunnel variable somewhere. I have seen a few examples online of some users using, instead of my $db line :
$db = new PDO('mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=myawsdbname,'myusertoaccessmyawsdb','mypasstoaccessmyawsdb');
But this is not working, as I don't have access to this mysql.sock resource as it should be on the bastion host. It's using my own mysqld.sock from my local machine. But where is the $tunnel stream really stored ? I am not sure.
I am quite confused as if it is possible to do so, and if so, how.
If you know anything or find anything, please tell me, it would mean a lot.
ssh2_tunnel() returns a raw socket resource, which is not going to be usable by any MySQL client libraries. It does not create a tunnel like the ssh CLI binary does. Also Unix sockets and TCP are very different beasts, so I would suggest not trying to follow that thread any further.
I would suggest not attempting to establish the tunnel in the context of a PHP script at all, as every request will open another connection and tunnel, potentially creating a lot of overhead on the bastion's SSH server.
To create an SSH tunnel on the command line:
ssh -N -L 3306:your.rds.instance:3306 your_user#bastion.host
Now you should be able to connect via 127.0.0.1:3306 in your PHP script.
To close the connection/turn off the tunnel issue ssh -O exit your_user#bastion.host.
However, I would not suggest using SSH tunnels for anything intended to be unattended. This is because I have found SSH tunnels to be finnicky, and they do not re-establish themselves after an interruption unless you're using a wrapper script, which is another layer of kludge. So I would suggest this approach for local dev/remote access only.
For a persistent connection that you might want to use to connect services/websites over, I would suggest some form of VPN depending on your particular requirements. I believe you can also apply security groups to your RDS instances to simply whitelist connections from certain public IP addresses/networks, but use with caution.

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.

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