Ad

Socket Connection Is Not Established On AWS EC2

I have deployed my code with docker image and here is the code for the socket connection.

I got SUCCESSFUL connection in the local system but when I deploy the same code on EC2 it gives me the following error: enter image description here

I'm using express server.

Server:

var serverIO = require('http').Server(app);
var io = require('socket.io')(serverIO);
io.on('connection', function(client) {
    console.log('Client connected...', client.id);
})

var server = serverIO.listen(config.app.port, function(){
    console.log('server running on port:', config.app.port)
})

Client:

<!doctype html>
<html lang="en">
    <head>

    </head>
    <body>
        <h1>Hello World!</h1>
        <div id="future"></div>
        <form id="form" id="chat_form">
            <input id="chat_input" type="text">
            <input type="submit" value="Send">
        </form>
         <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.dev.js"></script>
    </body>
</html>

<script>
 // var socket = io.connect('http://127.0.0.1:8000'); //here I got connection 
 var socket = io.connect('https://liveURL:8000'); //here I got error
    socket.on('connect', function(data) {
        socket.emit('join', {email: "[email protected]"});
    });

  socket.on('broad', function(data) {
        console.log(data)
         $('#future').append(data+ "<br/>");
   });

  socket.on("new_msg", function(data) {
    console.log("here")
    alert(data.msg);
   })

 $('form').submit(function(e){
     e.preventDefault();
     var message = $('#chat_input').val();
     socket.emit('messages', {email: "[email protected]"});
 });
</script>
Ad

Answer

You need to set the connection secure flag to true.

 var socket = io.connect('https://liveURL:8000',{secure: true}); //here I got error
    socket.on('connect', function(data) {
        socket.emit('join', {email: "[email protected]"});
    });

You can check the offical documentation.

{
  headers: /* the headers sent as part of the handshake */,
  time: /* the date of creation (as string) */,
  address: /* the ip of the client */,
  xdomain: /* whether the connection is cross-domain */,
  secure: /* whether the connection is secure */,
  issued: /* the date of creation (as unix timestamp) */,
  url: /* the request URL string */,
  query: /* the query object */
}

Or if you are using express on the server side then you can look here and here

Updated:

I saw that there is no SSL Configuration on your server-side so https will not work.

Try with http instead

io.connect('http://liveserver:8000')
Ad
source: stackoverflow.com
Ad