Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get my Node/Socket.io chat to communicate when it's on a domain?

I have successfully written a very simple real-time chat out of Node JS and Socket.io. It also uses Express, Jade and Stylus - which I want to develop further.

The current code for this is here (this works locally): https://github.com/littlejim84/basic_node_socket

This is all running fine and works as expected on my local machine. Running the Node app and going to http://localhost:9000/ makes the thing work as expected. But when I put it up on my remote server, the socket wasn't connecting. I had setup Ngnix to server my Node app, something like this:

upstream basic_node_socket { 
    server 127.0.0.1:9000; 
} 

server { 
    listen   80; 
    server_name  example.com; 

    location /basic_node_socket { 
        root /var/www/example.com/basic_node_socket; 

        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header Host $http_host; 
        proxy_set_header X-NginX-Proxy true; 

        proxy_pass http://basic_node_socket/; 
        proxy_redirect off; 
    } 
} 

With this I could go to the web address and it would server up my Node app, as expected, but the socket just won't connect. I'm presuming it's because on the client side now, I am listening to port 9000, which maybe has no relevance anymore now it's being served this way?

In my client side script I have this (which does work locally):

socket = new io.Socket(null, {port: 9000});

I'm not an expert at Node, I'm trying to wrap my head around it. Can anyone help me get this to work as expected on my remote server?

Any help would be greatly appreciated.

UPDATE: I have seen this link which seems to detail my problem: http://community.webfaction.com/questions/3448/using-websockets-with-a-nodejs-custom-app

...this is fine and I understand I will probably need a dedicated IP so I can run Node purely on port 80 and have one of my domain names point to that IP (or I could use something like Nodestar). But I want to keep things on my own VPS. This seems good, as long as I have one Node app running. But what if I want two, or three Node JS apps running on that new dedicated IP address? That's the bit I don't understand. I've seen various vhost type setups for Node JS, but I'm not really sure how best to implement that cleanly.

like image 503
littlejim84 Avatar asked Nov 25 '22 12:11

littlejim84


1 Answers

I'm not sure if this is your problem and haven't dealt with this first hand, but my understanding is that you cannot proxy websockets with nginx. That's probably why you are having troubles with websockets but not other requests.

Try using: https://github.com/nodejitsu/node-http-proxy

I've also heard that lighttpd works with websockets.

like image 112
Tauren Avatar answered Nov 28 '22 02:11

Tauren