An Issue with an AWS EC2 instance when connecting it to WebSocket

amazon ec2amazon-vpcamazon-web-serviceswebsocket

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

Best Answer

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 :)

Related Topic