Reputation: 1865
So I have been searching on this for quite some time now and tried a lot of things but just couldn't figure out how to make it work. I created a PHP WebSocket server (ratchet) on my local machine using XAMPP and an angular (v5) frontend, worked without any issues.
Then I tried to deploy both on a DigitalOcean instance where a LAMP stack is running because the app is more than just the WebSocket chat and the rest (a RESTful API) is working fine, but when the JavaScript tries to connect to the WS I get
WebSocket connection to 'ws://<ip-address>:8888/chat' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
So what I did do was to enable the apache2 mods proxy
and proxy_wstunnel
and created an entry for a vhost
<VirtualHost *:8080>
DocumentRoot /var/www/html_backend/web
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html_backend/web">
AllowOverride All
Allow from All
</Directory>
ProxyRequests Off
ProxyPass "/chat/" "ws://localhost:8888/"
# also tried 0.0.0.0 and 127.0.0.1 instead of localhost
</VirtualHost>
The PHP server socket is started like this
public function actionStart() {
$app = new App('localhost', 8888, '0.0.0.0');
$app->route('/chat', new Chat());
$app->run();
}
And in JS I try connecting to this
this._socket = new WebSocket('ws://<ip-address>:8888/chat');
I also tried it on port 8080 where it should be proxied from, but then it returned a 404 error.
On port 80 I have my frontend and on port 8080 my backend, which is working so far. I can start the WebSocket on port 8888 which did not give me any errors I detected but when trying to connect to it I get the upper error message. This is just for testing and I don't have an SSL cert yet but first I wanted to get this to work or do I require one? I tried lots of URL combinations found all over the web but none was working so far.
Just while writing I noticed that the socket is started from /var/www/html_backend
and not /var/www/html_backend/web
may the DocumentRoot
cause issues with proxying?
I hope someone can point me in the right direction because all "working" solutions I found were not so helpful thus far.
Best Regards
Upvotes: 0
Views: 2377
Reputation: 196
I build Websocket with PHP Linux and ratchet at Digitalocean and its working relay good, (my example its with wss but you can do it also with ws).
the way i build it:
Server side ratchet websocket:
$wsServer = new WsServer(new Chat);
$server = IoServer::factory(
new HttpServer($wsServer),
8000
);
Apache httpd-ssl.conf:
<VirtualHost _default_:443>
ProxyPass /wss2/ ws://your.domain:8000
Your js file:
wss://your.domain/wss2/
than just run php websocket in ratchet with command: php server.php
and you good to go..
Upvotes: 1