3

As I tried to run the chat app from localhost connected to MySQL database which had been coded with PHP via WebSocket it was successful.

Also when I tried to run from the PuTTY terminal logged into SSH credentials, it was displaying as Server Started with the port# 8080

ubuntu@ec3-193-123-96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$ php websocket_server.php
PHP Fatal error: Uncaught React\Socket\ConnectionException: Could not bind to tcp://0.0.0.0:8080: Address already in use in /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/vendor/react/socket/src/Server.php:29
Stack trace:
#0 /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(70): React\Socket\Server->listen(8080, '0.0.0.0')
#1 /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server/websocket_server.php(121): Ratchet\Server\IoServer::factory(Object(Ratchet\Http\HttpServer), 8080)
#2 {main}
thrown in /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/vendor/react/socket/src/Server.php on line 29
ubuntu@ec3-193-123-96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$

So I tried to change the port#8080 to port# 8282, it was successful

ubuntu@ec3-193-123-96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$ php websocket_server.php

Keeping the shell script running, open a couple of web browser windows, and open a Javascript console or a page with the following Javascript:

var conn = new WebSocket('ws://0.0.0.0:8282');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

From the browser console results:

WebSocket connection to 'ws://5.160.195.94:8282/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

I even tried to assign Public IP and Private IP, but no good it resulted in the same old result?

How am I suppose to resolve/overcome while connecting it from the WebSocket especially from the hosted server with the domain name such as http://ec3-193-123-96.eu-central-1.compute.amazonaws.com/

var conn = new WebSocket('ws://localhost:8383');

How do we need to set up Virtual Server to run your own services in an AWS EC2 instance?

Update:

From the Security Group

  • Under Inbound tab enter image description here

  • Under Outbound tab enter image description here

  • Hi Nishanth, if the response below answered your question please upvote and accept it. That's the ServerFault's way to say thank you for the time and effort someone took to help you. Thanks! – MLu Nov 03 '18 at 03:59

1 Answers1

2

Check that EC2 Security Group associated with the Instance permits access to all your ports, 8282, 8383, etc.

Also what's the IP or hostname your JS code tries to connect to? Is it the EC2 instance Public/Elastic IP? It should be. Nothing else will work.

Usually the JS code calls back to the same server that served the web page, i.e. if the URL is https://www.example.com/ the JavaScrips would then connect back to ws://www.example.com:8282/ - don't hardcode the hostnames, instead use whatever was used in the URL. It may be the IP address, the canonical name or some ec2-12-34-56-78.eu-west-2... address, all that should work regardless of what the user enters.

Hope that helps :)

MLu
  • 23,798
  • 5
  • 54
  • 81
  • Updated my post with screenshot. I think it has set right? – Nɪsʜᴀɴᴛʜ ॐ Oct 17 '18 at 06:54
  • @Nishanthॐ yes that looks ok, updated the answer with more hints about JS hostnames. – MLu Oct 17 '18 at 07:59
  • Tried still getting the same errors WebSocket connection to 'ws://ecx-x-xxx-xxx-xx.eu-central-1.compute.amazonaws.com:8282/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT – Nɪsʜᴀɴᴛʜ ॐ Oct 17 '18 at 08:11
  • What’s the exact IP? I’ll try to connect from here. – MLu Oct 17 '18 at 08:12
  • Since the fault lies neither with the Public/Elastic IP/DNS nor with the security group after rebooting the instance it had solved. It took two days to figure it out. This was the answer none of them had documented the root cause whether why without restarting the instance it might not be got solved. You could possibly edit your answer with researching those facts. So that I would gladly accept it and other readers would benefit a lot from your answer. – Nɪsʜᴀɴᴛʜ ॐ Nov 03 '18 at 04:55