I’ve made a small application that has a chat feature using node.js + socket.io to communicate. It works perfectly on my local ubuntu machine but I can’t seem to getting working on my droplet. The websocket communication just times out.

I’ve tried following multiple tutorials including http://nginx.com/blog/nginx-nodejs-websockets-socketio/ to no avail. I’ve spent two days so far on this. Any help would be greatly appreciated.

The app is written using the php framework Laravel, and node.js + socket.io is being used on the back end. The /etc/nginx/sites-available/default file is as below by default: (I have reverted all my attempts at fixing the problem)

server {
listen 80 defaultserver;
server
name default;
root /home/forge/default/public;

# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

index index.html index.htm index.php;

charset utf-8;

location / {
  try_files $uri $uri/ /index.php?$query_string;

  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

access_log off;
error_log  /var/log/nginx/default-error.log error;

error_page 404 /index.php;

location ~ \.php$ {
    fastcgi_param APP_KEY "SomeRandomString";
    fastcgi_param APP_DEBUG "true";
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

location ~ /\.ht {
    deny all;
}

}

2 comments
  • Were you ever able to find a solution? I’m having the same issues.

  • Hey @kevnk,
    the solution had to do with setting up the ports required for the communication.
    By default, all ports except :80 are blocked.
    I’m sorry, but it’s been too long for me to remember the specifics.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

I realized by default digital ocean droplets have ports closed.

I opened up the port to fix my timing out issue using the following command:
iptables -I INPUT 1 -i eth0 -p tcp –dport 8080 -j ACCEPT

I added a proxy pass in my nginx config: (replace PRIVATE_IP with your private IP)

    location /socks/ {
        proxy_pass http://PRIVATE_IP:8080/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

Then when setting up the node app on the same server I had to configure socket.io with that /socks/:8080 namespace like so: (notice io.of('/socks/:8080');)

var http = require('http')
var server = http.Server();

var io = require('socket.io')(server);
var nsp = io.of('/socks/:8080');
var Redis = require('ioredis');
var redis = new Redis();

redis.subscribe('test-channel');

redis.on('message', function(channel, data){
  console.log('Message received', arguments);
  message = JSON.parse(data);
  nsp.emit(channel + ':' + message.event, message.data);
})

server.listen(8080, process.env.SOCKET_PRIVATE_HOST)
Submit an Answer