5

I am using Ratchet-lib/socketo.me as a websocket for my chatting app. When I put localhost:8080 it works perfectly.

How can I put my wesbite as wss:// when I publish the app online? How to open port pr smthg?

This is the connection code :

$(document).ready(function(){
   update_chat_history_data();
    var conn = new WebSocket('wss://localhost:8080');
conn.onopen = function(e) {
    console.log("Connection established!");
};

I want to change var conn = new WebSocket('wss://localhost:8080'); with var conn = new WebSocket('wss://mywebsite:port');

Thanks

ILally
  • 319
  • 1
  • 8
  • 13
Ali Jand
  • 111
  • 1
  • 3
  • 11
  • So...what's stopping you from changing it? I don't understand the issue. Did you deploy the PHP code on your website's server and start it running? – ADyson Oct 26 '20 at 20:39
  • im a newbie at websockets so ... all im trying to do is when i test the app messaging on localhost with the server running with the composer everything is great ! now i published my website online how can i start a server or make it run ? i tried to put my domain name instead of the localhost and its says "failed: Connection closed before receiving a handshake response" – Ali Jand Oct 26 '20 at 20:43
  • composer is a package manager, it doesn't run servers, I don't know what you mean by that. You make the site run the same way you make it run in your localhost (assuming you did it from the command line??) - like in the ratchet demo: http://socketo.me/docs/hello-world – ADyson Oct 26 '20 at 20:46
  • yes exactly ! i used the command line for the localhost ! – Ali Jand Oct 26 '20 at 20:46
  • Ok well you need to do the same on your website server. (If you've bought cheap shared hosting, you may not have permission to do this, though. You may need a VPS or a cloud setup.) – ADyson Oct 26 '20 at 20:47
  • aahh okay , i didn't get a domain yet im testing it on a free subdomain hosting i think thats why it dosent allow me ! ... so when i get a full paid hosting , i will be able to run the server as i did with the command line ?? then i can use wss://mydomain ?! – Ali Jand Oct 26 '20 at 20:50
  • "when i get a full paid hosting"...it depends what you purchase. Like I said, cheap shared hosting is probably not going to support what you need (whether you have your own domain or a subdomain is largely irrelevant to that - it's the operating environment which is important). Make sure you purchase something which allows you to run things from the command line, and for them to stay running indefinitely. And as the answer below notes, you may need reverse proxy capabilities as well. If you're unsure if a hosting product offers that, check with the company's technical support – ADyson Oct 26 '20 at 21:04

1 Answers1

7

If you are using nginx in your production environment and it has by default ssl enabled meaning (https). Than you can do reverse proxy to your ratchet server.

upstream websocketserver {
    server ratchet:8080;
} 

server {

listen 443 ssl;
#you main domain configuration

location /ws/ {
            proxy_pass http://websocketserver;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_read_timeout 86400; # neccessary to avoid websocket timeout disconnect
            proxy_redirect off;
    }

}

Than you will be able to use ratchet url like: wss://yourdomain.com/ws

This is how it works with nginx but I guss is same with apache or some other web server. Just do reverse proxy!

AXheladini
  • 1,776
  • 6
  • 21
  • 42
  • It works good as a micro service. Lets say you have a container with nginx and another with ratchet. – AXheladini Oct 27 '20 at 08:17
  • 1
    hey bro , i couldnt figure out where to put this code ... can u tell me what to do step by step ? should i write it in my server.php or where ? – Ali Jand Oct 28 '20 at 19:27
  • Hello, no this is server configuration. At first can u tell me if u are using nginx or other server ? – AXheladini Oct 28 '20 at 19:49
  • as i said im still learning stuff , i can't understand what u mean by nginx , but now im using a free hosting to try my app , followed a ytb vid to install ratchet and make it worked , its doed work on my localhost (xampp) with the composer running the server.php (php server.php) , now im wondering after i published my website how can i get a ws or wss that can run my chatting app .. i tried wss://echo.websocket.org but it works fine but some variable from my json data shown as undefined .. – Ali Jand Oct 28 '20 at 22:14
  • With free hosting u cant do much. I assumed u have access to ur servers. With that I mean ability to configure the server where ur web is hosted. What is ur free domain name and what happens when u run php serve.php on this free hosting ? – AXheladini Oct 28 '20 at 22:18
  • im using awardspace.com , for the free hosting but i think i do not have access to modify the server settings , but nvm , if i get a premium hosting .. one day .. will i be able to creat my custom ws:// for the chatting app ? all i have to do is editing the server confg and then my website can accept websocket ? and one last question do u know any public ws// servers that can allow me to use it in my website withou limitation like ws://echo.websocket.org ? – Ali Jand Oct 28 '20 at 22:27
  • yes with access to the server u can configure it as u want. Also hosting company can help if it is dedicated hosting. – AXheladini Oct 28 '20 at 22:43