Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client connecting multiple times when proxied through nginx #36

Closed
ashokrkm opened this issue Mar 27, 2015 · 13 comments
Closed

Client connecting multiple times when proxied through nginx #36

ashokrkm opened this issue Mar 27, 2015 · 13 comments

Comments

@ashokrkm
Copy link

Sometimes it connects more than 3 times. If I connect directly to the socket.io it connects only once.

multiple connects:

    10.0.1.236 - - [27/Mar/2015:10:27:03 -0700] "GET /socket.io/?transport=polling&b64=1         HTTP/1.1" 200 100 "-" "socket/1 CFNetwork/711.2.23 Darwin/14.0.0"
    10.0.1.236 - - [27/Mar/2015:10:27:03 -0700] "GET /socket.io/?transport=polling&sid=vb1yUHmUr59l6el6AAAP HTTP/1.1" 200 4 "-" "socket/1         CFNetwork/711.2.23 Darwin/14.0.0"
    10.0.1.236 - - [27/Mar/2015:10:27:03 -0700] "POST /socket.io/?transport=polling&sid=vb1yUHmUr59l6el6AAAP HTTP/1.1" 400 45 "-" "socket/1         CFNetwork/711.2.23 Darwin/14.0.0"

Objective-C client code:

  - (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      socket = [[SocketIOClient alloc] initWithSocketURL:@"http://socket.xxx.com"       options:@{@"nsp":@"kwik"}];

      [socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) {
          NSLog(@"connected");

          [socket emitObjc:@"add-user" withItems:@[ @{@"username":@"username"} ] ];
      }];
      [socket on:@"data" callback:^(NSArray* data, void (^ack)(NSArray*)) {
          NSLog(@"data:%@", data);
      }];
      [socket connect];
  }

nginx config:

server {
    listen       80;
    server_name socket.xxx.com;
    location /socket.io {
        proxy_pass http://localhost:5000/socket.io;
        proxy_redirect off;
        proxy_buffering off;

        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
}
@ashokrkm ashokrkm changed the title Client connecting multiple times to socket.io when proxied through nginx Client connecting multiple times when proxied through nginx Mar 27, 2015
@nuclearace
Copy link
Member

I have no idea how to set up nginx, can you provide a server that does this? Also, does this work with the javascript client correctly?

@ashokrkm
Copy link
Author

sure, i can. is there a way to send a private message to you?

@ashokrkm
Copy link
Author

yes it works with javascript and also works with https://github.com/pkyeck/socket.IO-objc correctly

@ashokrkm
Copy link
Author

sent.

@nuclearace
Copy link
Member

Seems to be some kind of race condition with polling

@nuclearace nuclearace added the bug label Mar 28, 2015
@nuclearace
Copy link
Member

Workaround - have it force WebSockets

let socket = SocketIOClient(socketURL: "localhost:8080", options: ["forceWebsockets": true])

@nuclearace
Copy link
Member

Actually it looks like nginx is doing some kind of caching?

@ashokrkm
Copy link
Author

i have turned off the proxy buffering off

if i connect using https://github.com/pkyeck/socket.IO-objc
it works fine without any race condition and connects only once.

nginx config

    location /socket.io {
        proxy_pass http://XXXX:5000/socket.io;
        proxy_redirect off;
        proxy_buffering off;

        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

@nuclearace
Copy link
Member

I'll try to fix it tomorrow. Basically it's trying to poll constantly and getting a reply each time.

@ashokrkm
Copy link
Author

OK great. thanks.

it is a really basic setup.
here is the complete nginx server config, just proxies to socket.io running on port 5000 in localhost. if you want to try from your local machine anytime.

    worker_processes  auto;

    events {
        worker_connections  1024;
    }

    http {
        server {
            listen       80;
            location /socket.io {
                proxy_pass http://localhost:5000/socket.io;
                proxy_redirect off;
                proxy_buffering off;

                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_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
            }
        }
    }

@nuclearace
Copy link
Member

It is a cache problem.

@ashokrkm
Copy link
Author

thanks for the patch, it seems to be better, connections come to node instance only once.

But nginx still gets multiple requests. Is this OK?

   ==> node.log <==
   Sat Mar 28 2015 16:14:00 GMT-0700 (PDT): connected        CDdF50Bmvmqb3kxBAAL1
   Sat Mar 28 2015 16:14:00 GMT-0700 (PDT): added tEPn 1 clients now        connected

   ==> /opt/local/logs/access_kwik.log <==
   10.0.1.22 - - [28/Mar/2015:16:13:59 -0700] "GET /socket.io/?transport=polling&b64=1 HTTP/1.1" 200 100 "-" "kwik/1 CFNetwork/711.2.23 Darwin/14.0.0"
   10.0.1.22 - - [28/Mar/2015:16:14:00 -0700] "GET /socket.io/?transport=polling&sid=CDdF50Bmvmqb3kxBAAL1&b64=1 HTTP/1.1" 200 4 "-" "kwik/1 CFNetwork/711.2.23 Darwin/14.0.0"
   10.0.1.22 - - [28/Mar/2015:16:14:00 -0700] "POST /socket.io/?transport=polling&sid=CDdF50Bmvmqb3kxBAAL1 HTTP/1.1" 200 2 "-" "kwik/1 CFNetwork/711.2.23 Darwin/14.0.0"
   10.0.1.22 - - [28/Mar/2015:16:14:00 -0700] "GET /socket.io/?transport=polling&sid=CDdF50Bmvmqb3kxBAAL1&b64=1 HTTP/1.1" 200 15 "-" "kwik/1 CFNetwork/711.2.23 Darwin/14.0.0"
   10.0.1.22 - - [28/Mar/2015:16:14:00 -0700] "POST /socket.io/?transport=polling&sid=CDdF50Bmvmqb3kxBAAL1 HTTP/1.1" 200 2 "-" "kwik/1 CFNetwork/711.2.23 Darwin/14.0.0"

@oreace
Copy link

oreace commented Jul 7, 2020

i have turned off the proxy buffering off

if i connect using https://github.com/pkyeck/socket.IO-objc
it works fine without any race condition and connects only once.

nginx config

    location /socket.io {
        proxy_pass http://XXXX:5000/socket.io;
        proxy_redirect off;
        proxy_buffering off;

        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

THANK YOU SO MUCH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants