user341493
user341493

Reputation: 414

SocketIO over Nginx with SSL

I'm trying to setup socketio with ssl through nginx. The issue is that I can get the client to connect but then I'm not seeing the other events which I expect to come over the socket. (note: this does work locally just not on my production server)

the client code is here:

import openSocket from "socket.io-client";
const socket = openSocket(`${SOCKET}`);

function subscribeToTimer(callBack) {
  socket.on("timer", timestamp => callBack(null, timestamp));
  socket.emit("subscribeToTimer", 1000);
}

export class App extends Component {
  constructor(props) {
    super(props);
    this.store = this.configureStore();
    subscribeToTimer((err, action) => this.store.dispatch(action));
  }

and the server:

const port = 8000
const io = require('socket.io')()

io.on("connection", (client) => {
  console.log("a user connected")
  client.on("subscribeToTimer", (interval) => {
    console.log("a user is subscribing to timer with interval: ", interval)
    setInterval(() => {
      timestamp = new Date()
      client.emit('timer', { type: 'SET_TIME', payload: timestamp });
    }, interval);
  });
})

io.listen(port)
console.log('listening on port ', port)

which is managed by nginx /etc/nginx/sites-enabled/default:

server {
  <snip>
  location /socket.io {
    proxy_pass http://localhost:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

When I start the server, I get:

listening on port 8000
a user connected

So, the client is connecting to the server but I'm not seeing the subscribeToTImer event.

Any insights here?

Upvotes: 2

Views: 9175

Answers (2)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

The issue is probably because of two reasons. One is using the Host headers and one is using localhost instead of 127.0.0.1

server {
  <snip>
  location /socket.io {
    proxy_pass http://127.0.0.1:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
  }
}

I am not 100% sure of the root cause but I have seen removing Host and using 127.0.0.1 instead of localhost has helped in other issues with socket.io in past

Upvotes: 6

user341493
user341493

Reputation: 414

The problem turned out to be in the proxy_pass line of the config. You need to create an upstream section with a named group of servers and then reference that in proxy_pass (not http://localhost... the way I had it).

The working config /etc/nginx/sites-enabled/default:

upstream socket_nodes {
  ip_hash;
  server 127.0.0.1:8000;
}

server {

  <-- snip -->

  location /socket.io {
    proxy_pass http://socket_nodes;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
  }
}

Upvotes: 1

Related Questions