Socket.io acess from port 80 with apache reverse proxy on port 5000 from aws application load balancer

325 views Asked by At

I have one EC2 instance running behind AWS application load balancer. On Ec2 instance socket.io running on port 5000. Now I want to redirect domain/socket.io port 80 request to port 5000 from AWS application load balancer. domain I have mapped with AWS ALB ( CNAME ).

Now when I am connecting from port 80 outside, its working but not able to send message from outside to socket server. But when I am connecting directly on Port 5000 from outside then i can send message from outside.

I don't know why not able to send message from port 80 to port 5000. On EC2 instance using apache2.4 version so i have enable all necessary module.

My apache configuration is for socket.io is :

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io [NC]
    RewriteCond %{QUERY_STRING} transport=websocket [NC]
    RewriteRule /(.*)           ws://localhost:5000/$1 [P,L]
    ProxyPass /socket.io  http://localhost:5000/socket.io
    ProxyPassReverse /socket.io http://localhost:5000/socket.io

Please let me know how I can send message from outside to socket.io server on port 5000 using AWS alb.

Socket.io app.js file is below :

let app = require('express')();
    let http = require('http').Server(app); let io = require('socket.io')(http);

io.on('connection', (socket) => {

    // Log whenever a user connects
    console.log('user connected');

    // Log whenever a client disconnects from our websocket server
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });

    // When we receive a 'message' event from our client, print out
    // the contents of that message and then echo it back to our client
    // using `io.emit()`
    socket.on('message', (message) => {
        console.log("Message Received: " + message);
       socket.emit('message', {type:'new-message', text: message});    
    }); });

app.get('/', function(req, res){
    res.send('<h1>Hello world</h1>');   });  

// Initialize our websocket server on port 5000 http.listen(5000, ()
=> {
    console.log('started on port 5000'); });

Please help me regarding this.

0

There are 0 answers