NGINX configuration to work with Socket.IO

前端 未结 1 1786
旧巷少年郎
旧巷少年郎 2020-12-29 15:27

So I have been trying to get this to work 2 days and I am stuck. This is my first time configuring a server for rails that uses NodeJS+Socket IO. I am a noob with NGINX and

相关标签:
1条回答
  • 2020-12-29 15:34

    Updated Answer

    I have finally figured out a way to get this working. It's not at all obvious, but when hosting socket.io on a subfolder, you do NOT use the subfolder in the connect statement. This is what I was doing before and the client was never receiving a response.

    Not Working

    <script src="/test/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080/test/', {resource:'test/socket.io'});
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    </script>
    

    http://localhost:8080/test/ This is the part which is throwing things off. That creates a namespace for the local socket which the server side does not respect. So the client sends the message on the namespace '/test/' but the server responses are going to an empty namepace '' so the client never gets the messages. The workaround it to simply remove the '/test/' and make sure you are using the resource variable on the client and the server.

    Working!

    <script src="/test/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080', {resource:'test/socket.io'});
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    </script>
    

    I hope this helps you get things working on your end.

    Original Answer

    It is not a problem with your setup, it is a problem of socket.io not wanting to work on sub folder. I would bet willing to be that if you dropped the /sockets your example would work fine. I ran into the exact same problem when using http-node-proxy trying to host socket.io connections on subfolders. There was a bug created a while ago, but it was closed out and never resolved.

    https://github.com/LearnBoost/socket.io-client/issues/185

    https://github.com/LearnBoost/socket.io/issues/320

    I am still looking for a solution as well, but I have a feeling I'm going to have to roll up my sleeves and dig into the code myself.

    0 讨论(0)
提交回复
热议问题