PHP Ratchet can't connect from other device - php

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.

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.

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?

SFTP not connecting to remote server

I am trying to connect to a remote SFTP server using PHP. My code works fine when I connect to a local SFTP account but it times out for remote host. I have made sure through FTP client that host information is correct and its connecting fine.
I am using phpseclib library and my three line code is below.
require_once("phpseclib/Net/SFTP.php");
$sftp = new Net_SFTP('remote_host_IP');
var_dump($sftp->login('<username>', '<password>'));
It returns false (meaning not connected).
What I have done
I have whitelisted script in mod_security just in case its blocking that.
I have tried same script on my local computer and it connects successfully to remote SFTP.
Any valuable hint please?
Do define('NET_SSH2_LOGGING', 2) before initializing Net_SFTP and then do $sftp->getLog() after $sftp->login()
That'll provide enough info with which a diagnostic can be made.
This might help others. You need to make sure TCP_OUT port is open on your remote server to make it work.
Thanks for the help!

Flash socket server only works on local machine

I have created a basic flash socket server in PHP, it all works fine on my local machine (both in the browser and in the flash sandbox) but as soon as I get another machine to connect to my computer's Site directory, they can't get a connection open to the server!
In flash:
mySocket.connect("localhost",9999);
In PHP
$address = '127.0.0.1';
$port = 9999;
I have tried changing the flash code to the IP of my machine but then none of the clients (including the local) can connect.
Please help!
PS. Running on a mac in the Sites dir.
I am running the socket server through
terminal (PHP 5). Am running a policy file server with perl but I don't think that is the problem
"127.0.0.1" is the address for the loopback interface. It's only available on the same machine. You need to listen on all interfaces (loopback, eth0, etc) by binding the socket to "0.0.0.0".

Categories