Question

Socket.io connection timing out

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 default_server; 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;
}

}

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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)