F
F
fred55rus2017-01-12 15:31:58
PHP
fred55rus, 2017-01-12 15:31:58

How to make a websocket accessible from the internet?

There is a websocket in php using the ratchet library, only the connection code is probably interesting here:

require 'vendor/autoload.php';  
use Ratchet\MessageComponentInterface;  
use Ratchet\ConnectionInterface;
require 'php/process.php';
// Run the server application through the WebSocket protocol on port 8080
$app = new Ratchet\App("localhost", 8080, '0.0.0.0', $loop);
$app->route('/chat', new Chat, array('*'));
$app->run();

It is launched via the command line, and I connect to it via js: new WebSocket("ws://localhost:8080/chat")
This is how it works.
I can also connect from another computer from my local network if I replace localhost in both php and js code with the local host address (192.168.1.63 in my case).
However, if I substitute my external ip (2.60.19.4 for example), then I cannot connect either from the local network or from the global one. It would be ideal to somehow correct the situation at this stage.
Then I looked for an alternative way and found it in connecting to websockets through a request to the server. This gave me that I can connect to my websocket via apache, thanks to ProxyPass (mod_proxy_wstunnel is unlocked):
ProxyPass /ws/ ws://localhost:8080/chat
ProxyPassReverse /ws/ ws://localhost:8080/chat

BUT! this only works from the computer on which the server is enabled, that is, only like this: new WebSocket("ws://localhost/ws/")
If I substitute my local or global address instead of localhost, then the connection does not occur.
Then I switched to nginx and the result improved, I could connect not only from the host, but also from other computers on the local network, but from the Internet, using my external ip, I still can’t connect. In nginx, I had this config in nginx.conf for this:
location /chat/ {
  proxy_pass http://localhost:8080/chat;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

And I connected accordingly: new WebSocket("ws://192.168.1.63/chat/")
But none of the options allowed me to access my websocket from the Internet.
Here it is also probably important to note that I can connect to my server both from the local network and from the Internet, the problem is only with the web socket.
What do I really need so that people from the Internet can connect to my websocket?
Complement
8080 port is open on the host 162580f6ea1f4dc28d85976457bf459a.pngComplement #2
If I access the server, it returns a 404 error, in mozilla it looks like this: c55150a7325d4ce5b6c9b4fe3827cb36.png
If I try to access the socket directly, then I get a "connection refused" error from the browser, in chrome it looks like this: 94221b86c9454cf6ac3bdbc7eabdfcd5.png
The connection I'm testing here: www.websocket.org/echo.html

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Ras.su, 2017-01-12
@rassu

forward the port through the router where the web socket works.
Then access the web socket, from the outside, like:
new WebSocket("ws://2.60.19.4/chat/")
where 2.60.19.4 is your external ip, also use it with port 8080 or 80, as already required.
Check what is listening on the forwarded port 8080 and 80 of the Apache or nginx config that proxies the web-socket. times 404, then the request goes to the wrong vhost.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question