Websocket: Cannot establish a connection with basic websocket server - php

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?

Related

Connect to Mlab in Host

I tryed connect to Mlab with php by following code:
$connection = new MongoClient('mongodb://user:pass#ds051605.mlab.com:51605/db');
This code worked correctly in my system but didn't work in host and reterned Error 500 (my host doesn't support from mongo). What is the problem? Is there need to supporting mongo in host?
Additionaly, var_dump($connection) command doesn't show error.

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 connection works in local machine but not in server

I am using the Laravel Framework and BrainSocket to open up a websocket service in a port via the following command line:
php artisan brainsocket:start --port=7778
Afterwards, I connect to the websocket on the client-side, like so:
var websocket = new WebSocket('ws://127.0.0.1:7778/');
At first I was developing this application on my local machine and everything was working fine. However, on the production server (CentOS 7 + Nginx), when I run the above line the following error message appears:
WebSocket connection to 'ws://127.0.0.1:7778/' failed: Connection
closed before receiving a handshake response
I tried changing the IP for the server's IP address, I have already checked if the port was being used and I made sure socket support was enabled. But whatever I do the same error message appears.
It worked once I used port 443 instead.

Connecting to a WebSocket PHP server (code 1006 "Abnormal Closure")

I cannot establish a connection with the PHP WebSocket server, client closes it with code 1006 "Abnormal Closure".
Client code:
var host = "ws://example.com:1234/test/server.php";
Server Code (test/server.php):
$host = 'tcp://example.com:1234';
I tried different WebSocket PHP libraries.
I tried different hosts ("127.0.0.1", "0.0.0.0", "example.com").
sudo netstat -plnt shows that port is listened.
Appropriate TCP port is opened (firewall settings in ISPConfig 3).
But connection still not working. Any ideas?
Thank you in advance.

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!

Categories